#### Archived

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

# How does this junk work?

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

## Recommended Posts

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?

##### Share on other sites
quote:
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??

##### Share on other sites
quote:
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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 15
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632602
• Total Posts
3007364

×