had a look online but do not understand it at all anyone care to help??
http://en.wikipedia.org/wiki/Bit_mask
basically you have some data that is made up of a number of fields. Each of these fields are either True or False. Instead of storing them seperately in a database (for example) you can store them in one binary string.
You read the binary string out of the DB, you then perform an AND operation on that string with the value you are looking for. If you want to see if the 5th bit in the string is true or flase you do the following:
100111010 AND 000010000 = 000010000
In this case the result is equal to the mask you used, meaning that bit is True. To set it to False you do this:
100111010 AND 111101111 = 100101010
to set it back to True you do this:
100111010 OR 000010000 = 100111010
If you don't care what the value is, you just want to chang eit to the oposite value you use an XOR:
100111010 XOR 000010000 = 100101010
that any help to you?
Simple data is commonly stored as a set of flags or short bit sequences. I.e. you can store 4 flags and 1 4-bit number in a single byte, then access these data using bit masks to filter out the relevant datum.
Eg:then <data byte> AND 10000000 gives you the value of flagACode:FlagA FlagB FlagC FlagD Number 1 0 1 1 1011
So the bit mask specifies which piece of a set of data you want to look at.
It is quite complex, took me ages to get my head round it when a friend was trying to explain it to me a few years back. I was going to be using it in a database i was building at the time. Instead of having 20 (or so) True/False fields in a table, i would have a single "settings" field that could be an integer (number) field. If you treat this number as a string of binary, which is all it is, with each binary position as a unique piece of True/False data, you are able to store everything very efficiently. Another upside of this is you are only reading or storing to one field in a DB, so accesses could, in theory, be quicker. Of course you do have to do more to the field to decode the data, but that will be far quicker than pulling the data from the DB.
T is True, F is False, Fx is the Field name
Code:F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | --------------------------------------------- 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | = F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | --------------------------------------------- T | F | F | T | T | T | F | T | F | = | Settings | ------------- | 100111010 | (in binary) = | Settings | ------------- | 314 | (in decimal)
Either useful for software sprite handling (now *that's* been a long while since i've done that!) or as Funkstar says - for masking out values for checks... very handy in bit based operations (I seem to remember using this method for a house alarm project for college )
never done any sprite handling. good to know i wasn't the only one that started a house alarm project though
There are currently 1 users browsing this thread. (0 members and 1 guests)