#### Archived

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

# Using bitwise operators &!

This topic is 5761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well I really don''t know if & is called a bit wise operator (so why did I post that as my subject?!?), but what I really want to know is about this piece of code: //******************************* #define ALT_STATE_BIT 0x20000000 if (key_state & ALT_STATE_BIT) { // do something } // end if //******************************* Being programmed under a WIN32 environment, WHAT IS THE FUNCTION OF THE & THERE??? COULD SOMEONE TRANSLATE THAT TO ENGLISH? is 0x20000000 hexadecimal notation? if so, what is the first zero, before the x about? Am I logically ANDing the bit values? Does that return true if they are the same? I''m kind of confused right here... Oh, by the way should I stick to OpenGL or DirectX?

##### Share on other sites
The & checks if the bit 0x20000000 is set. 0x <--states that the number is in hex.

##### Share on other sites
quote:
Original post by dannyxyz23
Well I really don''t know if & is called a bit wise operator (so why did I post that as my subject?!?), but what I really want to know is about this piece of code:
//*******************************
#define ALT_STATE_BIT 0x20000000
if (key_state & ALT_STATE_BIT)
{
// do something
} // end if
//*******************************
Being programmed under a WIN32 environment, WHAT IS THE FUNCTION OF THE & THERE??? COULD SOMEONE TRANSLATE THAT TO ENGLISH?

It''s bitwise anding.
quote:

Yep.
quote:
if so, what is the first zero, before the x about?

It''s how you specificy a hex value.
quote:
Am I logically ANDing the bit values?

Yep, you''re anding each bit pair.
quote:
Does that return true if they are the same?

No. It''ll return zero if the key_state does not have the bit in position 13 set (the only bit that is set in 0x20000000).
quote:
I''m kind of confused right here...

And no wonder. It looks like you''re biting off quite a bit more than you can chew. Learn C/C++ FIRST, and then once you''re a good programmer, only THEN start working with graphics.
quote:
Oh, by the way should I stick to OpenGL or DirectX?

No, you should stick to C/C++ for now. Baby steps, man.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
Thank for your precise answers. But I didn''t got only one detail which is:

It''ll return zero if the key_state does not have the bit in position 13 set (the only bit that is set in 0x20000000).

How do you know it''s position 13? (I know I should be reading something about hex notation, but doesn''t it mean (2 * 16^0)=2, or am I reading it the wrong way, being (2 * 16^7) = 536,870,912?

But, seriously, in a LONG COMING future what Library should I use?

Thanks again

##### Share on other sites
It''s position is 13 because 2^13, expressed in hexadecimal, is 0x20000000.

The reason you''ll often see bitwise operators used with hexadecimal is because hexadecimal translate easily back and forth with binary. each hexadecimal digit corresponds exactly to four binary digits. The binary representation of 0x20000000 is:
0010 0000 0000 0000 0000 0000 0000 0000
I''ve broken up the binary number into groups of four so you can see how they correlate. Digit positions are counted from right to left, starting at 0. so the 1 is in the 13th place.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
Oh, and as for the library: My opinion is, go with either. they''re pretty close together in functionality, especially when you''re starting out.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
Oh, I think I got it...
But...(there''s always a but)
Shouldn''t it be the 29th position, instead of 13th?
I mean the easy conversion from hex to bin makes much sense, so, as there are 7 zeros (7*4 = 28, standing for 27th) and one 0 (28th) and one 1 (29th), isn''t it?
Well I hope it''s so, else I didn''t get anything...

##### Share on other sites
2^13 expressed in hex is 0x2000, not 0x20000000.

2^29 in hex is 0x20000000.

But I guess the answer depends if we are interpreting 0x20000000 as one 4-byte number or two 2-byte numbers.

##### Share on other sites
quote:
Original post by dannyxyz23
Oh, I think I got it...
But...(there''s always a but)
Shouldn''t it be the 29th position, instead of 13th?
I mean the easy conversion from hex to bin makes much sense, so, as there are 7 zeros (7*4 = 28, standing for 27th) and one 0 (28th) and one 1 (29th), isn''t it?
Well I hope it''s so, else I didn''t get anything...

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
Thanks again Sneftel and everyone else!

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633348
• Total Posts
3011463

×