a bit is either 1 or 0, a byte holds 8 bits, for example: 1 byte = 11111111
So, for speed and size sake, lets say we have a game with 2 enemies and we want to store their states (ie. dead, rest, visible etc..) we would normally need 3 boolean or int values to store these
char dead[2], rest[2], visible[2];
this takes up 6 bytes! if you use bits to store this info, like so
bit 0 = dead, bit 1 = rest, bit 2 = visible, then we would only need to use 2 bytes
char enemy[2];
now, if we want enemy one to be alive, resting and visible then we would assign him a value of
enemy[0] = 0x03; //00000011
and you want enemy 2 to be dead, not resting and not visible
enemy[1] = 0x04; //00000100
Bits can decrease program size and increase program speed.
bitwise operands are used to set these bits, for example, you have enemy 1 that is alive, resting and visible 00000011 and your hero went and shot him! So now he needs to be dead, not resting and not visible 00000100. You would do something like this.
#define STATEDEAD 0x04
enemy[0] = enemy[0] ^ enemy[0]; //clear state (^ means XOR)
enemy[0] = enemy[0] ^ STATEDEAD; //declare dead
This is a very rough example and text on this subject, but only because of my time contraints at the moment. So, if this confuses you let me know and I'll try to explain it better when I get a chance.
One small correction: In C a byte is not necessarily 8 bits. The number of bits in a byte is guaranteed to be at least 8, but could be more. It is equal to CHAR_BIT found in <limits.h>
Bitwise says bitwise operations are cool. Well, bits have already been discussed by PSkYiClHlOeDrIC (?). But bitwise operations can be used for a lot of things namely to speed things up or to store lots of information in one variable. A lot of times the bitwise | or is used to group a bunch of options together into one variable (usually a 32 bit variable). For example. I could define some constants:
Now I could have a function that could use zero or all of these constants by doing something like this:
void MyFunction(unsigned int options)
{
...
}
I could pass values to this function like this:
MyFunction(MY_CONST2 | MY_CONST1);
or
MyFunction(MY_CONST1 | MY_CONST2 | MY_CONST3)
Now I would have one variable ('options') that contained everything I wanted the options to be. So when it comes time to see if I have something set I would go like this:
if(options & MY_CONST1)
{
/* option set do something in this case */
}
Pretty cool stuff huh.
Bitwise operations can also be used to speed math operations up (but ever so slightly as computers these days are so fast you won't notice a difference unless your working on Quake4). For example, shifting a binary number to the left is like multiplying by 2, and shifting to the right is like dividing by 2. For example:
int val = 4;
val >>= 1;
printf("%d\n", val);
val would be 2 b/c >> is like dividing by 2. Likewise change the line to val <<= 1 then val would be 8 b/c that is like mulitplying by 2. More "advance" math can also be done like: val*64 can be done by going val << 6 b/c 2^6 is 64! Bitwise operations are fun to mess with, and I've only touched the surface, but this is already far too long of a post.
If you write a windows program (for example)you combine bits:
CreateWindow(.....,WS_OVERLAPPEDWINDOW|WS_CHILD|WS_VISIBLE...)
It is much more economical than put different variables.
Bites are like swithers on/off. John Fill
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.