How does this junk work?

Started by
1 comment, last by BradDaBug 21 years, 8 months ago
I''m trying to figure out how to use bitwise stuff. I know this is true: 0x1000 & 0x1000 = 0x1000 0x1111 & 0x1000 = 0x1000 0x5001 & 0x5000 = 0x5000 So I figured "ok, it just compares each diget." Then I go and try something like this: 0x5200 & 0x2300 = 0x0200 which is wrong (to me anyhow) which just goes and confuses me. How does bitwise stuff work? I''m getting worried that it compares actual bits and using hex won''t help make it easier. If it compares actual bits, how can I possible come up with values that can be compared and get something meaningful? Here''s what I''m trying to do: #define SHOE_TYPE_1 0x1001 #define SHOE_TYPE_2 0x1002 #define BOOT_TYPE_1 0x2001 #define BOOT_TYPE_2 0x2002 #define SHOE_ANY_TYPE 0x1000 #define BOOT_ANY_TYPE 0x2000 So if my first theory about how this worked, I could have determined the type of particular footwear I was looking at with something like this: if (footwear & SHOE_ANY_TYPE == SHOE_ANY_TYPE) ItsAShoe = true; else ItsAShoe = false; I hope I haven''t completly turned my stupid question into gibberish. How does this stuff work?
I like the DARK layout!
Advertisement
quote:Original post by BradDaBug
So I figured "ok, it just compares each diget." Then I go and try something like this:

0x5200 & 0x2300 = 0x0200

which is wrong (to me anyhow) which just goes and confuses me.

You have to remember that each digit there is in hex and represents *four bits* (aka a "nybble"). This means your test expands to:

1001 0010 0000 00000010 0011 0000 0000-------------------0000 0010 0000 0000...=(0)  (2)  (0)  (0) 


Make any more sense now?

I''ll answer the other part tomorrow if nobody else does. It''s nearly 4:30 AM over here. What the hell am I still doing up at this time??
quote:Original post by BradDaBug
How does bitwise stuff work? I''m getting worried that it compares actual bits and using hex won''t help make it easier.


That''s why it''s called bit-wise. Unfortunatly, there''s no way you can write binary numbers directly in C/C++ (which is a shame in my opinion) but you can make it a bit easier on yourself by writing your constants in hex. (You can also write them in octal, which might make it even easier, but most people don''t and you''ll probably only end up confusing others )

Translating hex-to-binary and back is quite simple, which is why we generally use hex...

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

This topic is closed to new replies.

Advertisement