Jiri has gotten some good answers. I got to work late today after working
until 11:30 last night meeting my 5:00 deadline :-P

 

I did occur to me that a fair number of us may not completely grok bitwise
stuff. That's computer science stuff, and a lot of us got here by other
routes-I see a lot of C programmers doing bitwise stuff, but relatively few
AS programmers (at least, we don't talk about it a lot). Still, it can be
blazingly fast, so when you have a need for speed, they are great.

 

So I thought I'd talk a little about bit operations. If you already
understand them, no need to read further (unless you wish to check my
accuracy).

 

First, the basics. We all probably know this, but a byte is no more than a
series of on-off switches, or bits--eight of them on most modern computers.
On is represented by 1, off by 0. So, a byte with every other bit on would
be 10101010.

 

When you do a bitwise operation, you are comparing bits. The bitwise OR
operator, | , compares bits. If either of them is on (1), the result is 1.
If both are off (0), the result is 0. Consider the following:

 

120 | 96

 

Compares 10101010 and 01100000, giving the result 11101010 (234 decimal).
Visually, this may help:

 

10101010

01100000

________

 

11101010 

 

We get that result because the | operator compares each bit. If either of
them is on, the result is 1 for that bit.

 

The AND operator & is similar in that it compares bit by bit, but both bits
have to be on to get a 1 result. Comparing the same two numbers as before,
120 & 96, gives you the result 00100000, or 32 decimal.

 

10101010

01100000

________

 

00101010 

 

Sometimes we use these as flags, where every bit can represent a Boolean
value. They also can be used for fast math operations (check out
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/

 

This barely scratches the surface of bitwise operators and their power, but
I hope it intrigues some of you enough to pursue it further.

 

Cordially,

 

Kerry Thompson

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to