need some help with bitmasking

Associate
Joined
25 Nov 2004
Posts
1,944
can't quite get my head around what this is after about an hour searching, ive come up with it using significant data to extract data from another place...need some help guys :( dumb it down for me
 
riddler said:
can't quite get my head around what this is after about an hour searching, ive come up with it using significant data to extract data from another place...need some help guys :( dumb it down for me

Bit masking generally is the process of using the individual bits of a larger datatype to store data that doesnt require the whole space.

For example, in most modern languages, a boolean data type requires 1 byte (since this is generally the minimum addressable 'chunk' of data).

But obviously one byte can store up to 8 individual boolean flags, since it can be represented as an 8 digit boolean number from 00000000 to 1111111.

So if the bits are numbered from 0 at one end to 7 at the other, you can store the different flags as follows:

00110011: Flags 2,3,6 and 7 set, all the others unset.

A bitmask is used to find out which flags are set.

For instance, if I want to know if flag three is set, I can use the bitmask 00010000.

Then, by doing a bitwise AND, I can see if its set. Using the number above:

00110011
00010000

Bitwise ANDing the two together yields:

00010000

As this is non-zero the bit is set.

If I checked the 4th flag:

00110011
00001000

Bitwise AND yields zero.

Obviously through careful choice of the mask I can check more than one flag at a time.

Obligatory wiki link: http://en.wikipedia.org/wiki/Mask_(computing)
 
Back
Top Bottom