After talking to many engineers/programmers I am sort of shocked of their understanding of bits, bytes, hexadecimal and binary. They all use them but don’t really understand what is going on exactly. An understanding of bits is essential to game development (most peoples response to this is really??).
Let’s have a look at the number 256, it is a magical number. Let’s have a look at the relationship:
256 in hexadecimal is 0×100 and in binary is 1 0000 0000 (8 zeros). So what’s so magical about it. Well, 256-1 is 0xFF which is 1111 1111, all 8 bits are set, not quite magical yet. 256/2 = 128/2 = 64/2 = 32/2 = 16/2 = 8/2 = 4/2 = 2/2 = 1, so keep dividing by 2 gives you 1 exactly, nearly magical right? 256 can also be looked out in different ways, 2 pow 8 = 256 = 1<<8 (logical shift) = 256. 256 pow 4 = 4294967296 – 1 which we all know is the maximum 32 bit number 0xFFFFFFFF and in binary (32 x 1’s). Well I think it’s magical, perhaps you will one day.
So if we understand 255 (256-1) being 0xFF, that means if you stack 4 bytes together you get 0xFFFFFFFF (4294967295, ARGB, 32 bits set). Let’s say we want to break the number down into ARGB ( pixel, alpha, red, green, blue) components, we do it in this way:
var argb:uint = 0xFFFF8040; // full alpha, full red, half green, quarter blue
var alpha:uint = argb >> 24;
var red:uint = (argb >> 16) & 0xFF;
var green:uint = (argb >> 8) & 0xFF;
var blue:uint = argb & 0xFF;
Let’s break this down, what is argb >> 24? Basically it is moving the number down 24 bits:
1111 1111 1111 1111 1000 0000 0100 0000 (0xFFFF8040) >> 24
0000 0000 0000 0000 0000 0000 1111 1111 (0x000000FF) // result
And doing the same with the red >> 16:
1111 1111 1111 1111 1000 0000 0100 0000 (0xFFFF8040) >> 16
0000 0000 0000 0000 1111 1111 1111 1111 (0x0000FFFF) //result
Trouble with this is that the A part (Alpha) is still lingering above the red, this is where the & (bitwise And) comes in:
0x0000FFFF & 0x000000FF - this is not helpful, let's look at it in binary:
0000 0000 0000 0000 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 1111 1111
---------------------------------------
0000 0000 0000 0000 0000 0000 1111 1111
Why? Simply walk along the numbers if the bit is set in either then the result is bit set, if one value is 0 then the answer is 0. Perhaps you need more reading up on this http://en.wikipedia.org/wiki/Bitwise_operation.
0×000000FF >>8 = 0×0000FF00 (each digit is 4 bits, a nibble). Some crazy dude thought it would be interesting to use eating words for binary data, bit (1), nibble (1111) and byte (1111111), so why is an int not called a meal? Nevermind.
So how about some real usage, well I use these bits for collision filtering (a bit primitive, but essential to AS3 for performance):
var bPerson:uint = 1<<0; // 1
var bBuilding:uint = 1<<1; // 2
var bRock:uint = 1<<2; // 4
var bBarrel:uint = 1<<3; // 8
var bWall:uint = 1<<4; // 16
Let’s say each object has one of these variables assigned to something called flags, so a barrel.flags = bBarrel:
flags = bBarrel; // 1<<8 16 0x08 1000
var _compareFlags:uint = bBarrel|bWall; // 24 0x18 1100
if ( _compareFlags & flags) // 1000 & 1100
<do something>
So instead of saying is it a barrel or is it a wall or is it a person etc. It’s a single line check. And you can pretty much use this everywhere you want to do this kind of comparison.
I hope this helps in your future game dev… good luck!















