PDA

View Full Version : Bit Flags



godzcheater
04-18-2012, 07:52 PM
Well I first saw bitflags in castle crashers, but I just 0xFF'ed them then I saw them again in super meat boy a while ago, and they are easy enough to mod, a bit harder to code, but still too not hard.
What are bit flags

Basically its a way to store 8 Booleans(True/False) in one byte, instead of different methods that use more space.

Why do people use bit flags

As above it uses less space. for example in 1 byte you can use all 8 bits separately instead of all 8 bits for one bool, some games *plants vs zombies* even use Int32 (32 bits) just to store one boolean, when they could be storing 32 in that place.

What are bits

Im not going to go into detail because everyone already knows, but each byte is split into 8 bits that are On/Off in elctrcal signals witch is often shown as 1/0 or true/false.
for int format they are gently used (This is just byte but carrys on for bigger ints)128|64|32|16|8|4|2|1
so
00000000 = 0
00000001 = 1
10000000 = 128
10000001 = 129
11111111 = 255
etc

How to mod bits

If your using an advanced hex editor like hexworkshop the bits are displayed in the inspector bar, so you just need to change the specific bit, and it will update the byte.
for other basic hex editors like hxd you will need to get the int for the byte(0xFF = 255)
then convert the to bits like shown above, and back after changing your desired bit.

How to handle bits in .NET

Here is a quick snippet of mine to get bits,
[C#] Bits - Pastebin.com (http://pastebin.com/LnbTCm0a)