Advertisement

Help with the & bit operator and hex

Started by June 04, 2001 02:50 PM
9 comments, last by the_grip 23 years, 8 months ago
Could someone explain why this works:
  
//now check if the camera coordinates are divisble by 32...

x = mapdata.xCamera & 0x0000001F
y = mapdata.yCamera & 0x0000001F

if(!x)
{
      //if xCamera is divisble by 32...

      xEnd--;
}
else
{
    //otherwise move dest RECT to the left to clip the blah blah...

    rcDest.left-=x...you get the picture
}
  
i think i understand enough of hex and the & operator to know what is going on, but 0x0000001F is 31, not 32. i would think using the & bit operator in this case would produce weird results unless you do it to 0x00000020... i''m pretty new at this type of stuff... the more verbose the answer the better. "You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
0x20 = 100000binary. This means that one bit is set, and an "AND" instruction only "ANDS" that bit.

0x1f = 011111binary. When you ANDS with all those bit, you will get a value equal or greater than 0 and less than 32.

Fredrik
Fredrik Hellmanmrhellman@telia.com
Advertisement
0x20 = 100000binary. This means that one bit is set, and an "AND" instruction only "ANDS" that bit.

0x1f = 011111binary. When you ANDS with all those bit, you will get a value equal or greater than 0 and less than 32.

Fredrik
Fredrik Hellmanmrhellman@telia.com
0x20 = 100000binary. This means that one bit is set, and an "AND" instruction only "ANDS" that bit with the other operand.

0x1f = 011111binary. When you ANDS with all these bit, you will get a value equal or greater than 0 and less than 32.

A five bit wide value can never be 32, but it has 32 combinations of the bits.

Fredrik
Fredrik Hellmanmrhellman@telia.com
Could you explain a little more in depth... i''m green at hex/bit operations (maybe simply illustrate your explaintaion). Thanks for the help!

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
I am sorry for all the posts :D The server was slow and I pressed the send button several times :-)

I will try to explain, but my weak english and bad explaining skill will make it harder.

One hex number is using 4 bits (a nibble). Two uses 8 (a byte). This means that there are 16 combinations of bits / 4 bits (a nibble).

0xF = 1111, while 0x1 = 0001. If you will get 0x1f in bits, you just merge these eight bits: 0001 1111 <--- 0x1f <--- 31.

0x0 = 0000, 0x2 = 0010. 0010 0000 <--- 0x20 <--- 32.

The and operator takes two values, let''s call them A and B. A = 0x25, while B = 0x1f.

0x25 = 0010 0101 and 0x1f = 0001 1111.

Let''s AND these two values: 0010 0101
0001 1111 AND
-------------
0000 0101

If you instead of using 0x1f use 0x20 you will get the following result:

0010 0101
0010 0000 AND
-------------
0010 0000

This means that if you want the sixth bit, use 0x20 instead of 0x1f - but if you want all bits from the first to the fifth, use 0x1f.

I hope you understand, if not, send me an e-mail at mrhellman@telia.com

Fredrik
Fredrik Hellmanmrhellman@telia.com
Advertisement
read this.
i think it will make a few things clearer for you

Arkon
[QSoft Systems]
Don''t you think my explanations was good?!?! ;-) hehehe. I am no good at explaining at all.
Fredrik Hellmanmrhellman@telia.com
i think my question is with the if(!x) statement...

Why does that indicate the number is divisble by 32, whereas the opposite (if(x)) doesn''t?

i would think you would get if(x) if the number was divisble by 32...

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
I GOT IT!!!!!!

32 = 00100000
31 = 00011111 (from hex) - now & them together
-------------
0 = 00000000 heh, shows that 32 is divisible by 32, i.e. not x

30 = 00011110 &
31 = 00011111
-------------
30 = 00011110 i.e. not divisible by 32 i.e. x

WHOHOO!!! Thanks y''all for the help... that was really stumping me

Now, one last question... why use hex vs. just decimal? is it faster?

"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"

This topic is closed to new replies.

Advertisement