Explaining Hexadecimal numbering system

Disclaimer:
This post is actually from quite a long time ago but I never got around to update it and I don’t remember what I wanted to convey with this blog post.

 

In the past I had quite some difficulty with the different number systems. You will definitely get in touch with Binary and Hexadecimal systems if you continue along the road of programming therefore I really wanted to make a simple blog post about how exactly the Hexadecimal number numer system works.

First up let’s look at the meaning of the name: Decimal is the word we use for our regular number system which goes up to 10. Hex might sound familair from Hexagon which is a shape with 6 equal sides. So if you put these parts together you have a number system that goes up to 16.

The first 10 numbers are the same as our regular number system e.g. (0-9) and after it reaches 10 it continues with letters e.g. (a – 10, b – 11, c – 12, d – 13, e – 14, f – 15).

A common usage of hexadecimal numbers is in color codes because 16 times 16 = 256 and simple color systems use 3 sets of 256 shades(red, green & blue).

Therefore the Hexadecimal system might look confusing because it uses letters but in the end it is not that difficult!

Bitwise Tilemaps

Here is a technique that I have been wanting to use in my projects for a while: Bitwise Tile Generation. It’s usefull to add some flavor to your world/level.

Basically you use a full tileset for the grass or platformer tiles in your game and you calculate if there are similair tiles next to each other to change the sprites. If you don’t have to calculate corners you just have to check 4 tiles each time for 16 different combinations.

Bitwise 1

Bitwise Tile Generation

If  you only have a tile on top you will use sprite 1 in the middle. But it you have a tile to the right and on the bottom you would use sprite 6. There are 16 different combinations from no tiles (0) to all tiles(15).
The reason why this technique is called Bitwise is because you’re basically using bits to check if there is a tile or not. 0000 translates to 0 or no tiles. And 0001 would be a tile on the top and so on.

But what if you would like to check the corner tiles as well?

Bitwise 2

Bitwise Tiles with Corners

Wel you just need to use more bits to check every possible combination. With corners you will have a full byte with 256 possible combinations from 00000000 to 11111111.

It’s a very usefull technique to use and it saves you a lot of time compared to building the world manually!

Source/Extra links:
Saltgames blog: http://www.saltgames.com/2010/a-bitwise-method-for-applying-tilemaps/Flashpunk Forum post: http://developers.useflashpunk.net/t/flixel-compatible-tilemap-autoset/1736