How to check the #N bit of a s32/u32 number?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
CuteAlien
Admin
Posts: 9926
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

MasterGod wrote: I'm starting with 1 cause its more easy to think/say "the 7th bit is:" then "the bit in the 6th place is:"..
That's certainly up to you, but I'd recommend starting to count at 0. It's not really more difficult once you get used to it and you _will_ get used to it in the long run when programming :-)
Also when using bitoperations you often care about speed and adding the '-1' will make your operation slower.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Great, I got my answer - better then I expected :D

Thank you all!
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
drac_gd
Posts: 132
Joined: Sun Apr 09, 2006 8:43 pm

Post by drac_gd »

Would this work ?
#define isBitSet(num,n) (( num & (0x01<<n) ) ?1:0;)
#define setBit(num,n) ( num |= (0x01<<n) )
#define clearBit(num,n) ( num &= (0x01<<n) )

shift operators are computationaly cheap
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

#define clearBit(num,n) ( num &= (0x01<<n) )
Close...

Code: Select all

#define clearBit(num,n) ( num &= ~(1<<n) )
Note that you don't need to use hexadecimal notation for 1 [0x01 is the same as 0x1, 01 and 1].

Travis
drac_gd
Posts: 132
Joined: Sun Apr 09, 2006 8:43 pm

Post by drac_gd »

ahhh .. I see the error now.. thanks
Post Reply