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:
#define MY_CONST1 (1<<0)
#define MY_CONST2 (1<<1)
#define MY_CONST3 (1<<2)
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.
Later,
-bitwise
