Advertisement

bitwise operators

Started by March 16, 2000 08:26 PM
5 comments, last by Aztec 24 years, 7 months ago
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
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;


(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

Advertisement
Note: the slash in the last example is actually a vertical bar. The forum software converted it for me, as it apparently knows best

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

so, does the OR switch the bits?
1 / 1 would become 0 0 ?
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.
Chris
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
Advertisement
ahh, thanks guys, i get it now.

This topic is closed to new replies.

Advertisement