Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bits

Status
Not open for further replies.

chop

Programmer
Mar 29, 2001
4
0
0
US
Hi All,

Just a quick question here. Bits and bitwise operations, what are they used for?

Chop
 
Bits are used to hold information...

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>

Russ
bobbitts@hotmail.com
 
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(&quot;%d\n&quot;, 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 &quot;advance&quot; 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 ;)
 
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
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top