Archived

This topic is now archived and is closed to further replies.

mattd

Using bits as flags

Recommended Posts

mattd    1078
Argh! This seems so easy, but I just can't get it right. All i need to know is: 1) How to check to see whether a certain bit is 1 or 0 in a variable. 2) How to set a bit in a variable to 1 or 0. That's it! As you can tell, I'm using a unsigned char in a struct to allow BOOL-type flags. I thought the answers to these questions were...
   
#define ANIM_ISPLAYING 0x01
#define ANIM_LOOPANIM  0x02
#define ANIM_REVERSE   0x04
#define ANIM_RANDOM    0x08

// 1) How to check to see whether a certain bit is 1 or 0 in a variable.
if(m_ucFlags & ANIM_LOOPANIM)
    // this tile loops

// 2) How to set a bit in a variable to 1 or 0.
m_ucFlags &= ANIM_REVERSE;    // set the reverse bit
m_ucFlags &= ~ANIM_ISPLAYING; // un-set the isplaying bit
 
.. but i've seen tons of other things, and I'm all confused. Someone care to help? Thanks, -- mattd Edited by - mattd on July 28, 2001 6:12:58 AM

Share this post


Link to post
Share on other sites
Dean Harding    546
to test a bit (Note: you don''t really need the "!= 0" bit):

  
if( (m_ucFlags & ANIM_LOOPANIM) != 0 )
// ...



To set a bit:

  
m_ucFlags |= ANIM_REVERSE;


To un-set a bit:

  
m_ucFlags &= ~ANIM_ISPLAYING;


So you had it all right, except for the setting of a bit.


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
Kylotan    9853
Because you''re not setting a value, you''re setting individual flags. Flag one corresponds to 1. Flag two corresponds to 2. Flag 3 corresponds to 4. Flag 4 corresponds to 8. And so on. In Hex, after 0x08 you have 0x10 (16). Then 0x20 (32), 0x40 (64), etc...

Share this post


Link to post
Share on other sites
Zipster    2359
People use hex because you can represent larger numbers with less digits, and since base-16 is closely tied to base-2 (16 being 2^4), it''s more intuitive to catch on to. Would you rather write 524288, or 0x80000?

And to answer your question about specifiying binary, you would put the letter ''b'' after the number. So int x = 1011b; would set x to 11 decimal.

Share this post


Link to post
Share on other sites
Stoffel    250
quote:
Original post by Zipster
And to answer your question about specifiying binary, you would put the letter ''b'' after the number. So int x = 1011b; would set x to 11 decimal.

In MS assembler, yes. In C and C++, no. Unfortunately, there''s no way to do binary in C/C++. You can use octal--but that''s less intuitive than hex (IMHO).

Share this post


Link to post
Share on other sites
Zipster    2359
Oh yes that''s right... I''ve been working on my assembly lately so that''s fresh in my mind. I wrote a little inline function to do it for me

  
inline int BIN(char* binary)
{
int result = 0, pos = 0, power;

power = strlen(binary) - 1;

while(binary[pos])
{
if(binary[pos] != ''1'' && binary[pos] != ''0'')
return NULL;
result += pow(2,power) * (int)(binary[pos] - 48);

power--;
pos++;
}
return result;
}

I know it uses a string and it isn''t the most speedy thing in the world, but it works, and I might work on a newer, faster one.

Share this post


Link to post
Share on other sites
Zipster    2359
Oh wait, if we use macros, we can pull this off.

      
// This is bit of a hack, but it's fast and compact

// Used internal to make number + 'b' into a single token

#define MIX(number, b) number##b
// Use to make binary number

#define BIN(number, result) {_asm mov result, MIX(number, b)}


Then you can do:

  
BIN(1011, q)

And then q will be equal to 11.

Edited by - Zipster on August 1, 2001 3:13:06 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

// This is bit of a hack, but it''s fast and compact


Hey, that rhymes!
Yer a poet and you didn''t even know it!

Share this post


Link to post
Share on other sites