• ### Announcements

#### Archived

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

# Using bits as flags

## Recommended Posts

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 on other sites
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 on other sites
what does 0x mean?
does it mean ur number system is binary? if so, y 1,2,4,8 instead of 1,2,3,4?

##### Share on other sites
0x does mean hex values, not binary.

##### Share on other sites
oh, so how would u get a number to represent binary?
and if it''s hex, then y do ppl do 1,2,4,8 (in powers of 2)?

##### Share on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites

// 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!

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627653
• Total Posts
2978440

• 10
• 12
• 22
• 13
• 33