Jump to content
  • Advertisement

Archived

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

Using bitwise operators &!

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The & checks if the bit 0x20000000 is set. 0x <--states that the number is in hex.

Share this post


Link to post
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:
is 0x20000000 hexadecimal notation?

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...
Thanks for your patience...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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...
Thanks for your patience...


You''re completely right. My bad.


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!