bitwise operators
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.
(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.
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