Jump to content
  • Advertisement

Archived

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

Aztec

bitwise operators

This topic is 6643 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

what exactly do they do? i''ve not used them too much in my regular programming jive, but now that i''m getting more involved with win32 / direct x programming I see them more frequently. so, what exactly do they do? do they just turn the bit on / off depending on it''s current value? thanks for the help -Aztec

Share this post


Link to post
Share on other sites
Advertisement
Yep. They perform operations on bits.

For example, if I wanted to clear the rightmost bit in a 16-bit variable, I could use the following. . .

val = val & 0xfffe;

Or if I wanted to negate all the bits in a variable. . .

val = ~val;

Or if I wanted to OR the bits in two variables together. . .

val = val / val2;


Share this post


Link to post
Share on other sites
Note: the slash in the last example is actually a vertical bar. The forum software converted it for me, as it apparently knows best

Share this post


Link to post
Share on other sites
No, the OR in bitwise operators works like the OR in boolean operations, if you did 4 (00000100) / 2(00000010) you would get 6(00000110) and if you did 1 / 1 you get 1, it''s the same as (true // true) == true, the bitwise operators just set all the bits that are on in each of the two ORed variables or numbers into a single variable combining them.

Share this post


Link to post
Share on other sites
Hi Aztec,
To elaborate a little, there''s a few ways of looking at them: First of all, if you understand the logical operators (&&, //, !) and you understand binary represeantion, then you can view the binary operators as fine-grained versions of the logical ops. When you write ''(x // y)'' it looks at the value of x, treats it as true or false. It then looks at the value of y, treats it as true or false. Then, the result is true if x OR y is true.

With the binary version (/), x and y are not reduced to their truth values. Instead, they are converted to binary (this "conversion" doesn''t really happen, all of these steps are just for understanding the end result): so if x = 10 and y = 6, you get (with lots of zeros at the beginning):

x = 1010
y = 0110

Now, when you write ''(x / y)'' it goes through each bit of the two inputs and does the ''//'' operation to determine the output bit. In my example it goes something like:
- bit n = (x bit // y bit) = result
- bit 1 = (1 // 0) = 1
- bit 2 = (0 // 1) = 1
- bit 3 = (1 // 1) = 1
- bit 4 = (0 // 0) = 0

So the result of (x / y) (with x=10, y=6) is 1110, which converted back to decimal (that''s still in binary) is 14.

I hope this helps somewhat. I can think of at least one other way to explain this, but it still requires understanding binary representation of numbers, which is probably the biggest stumbling block. I''ll wait to see if you have more questions...

-Brian

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!