[Wolves] Bitwise AND

Aquarius aquarius-lists at kryogenix.org
Tue Jan 6 10:56:53 GMT 2004


Matthew Revell spoo'd forth:
> Howdy,
> 
> Can anyone give me a succint and easy to understand explanation of what a
> bitwise AND is?

Imagine two numbers in binary form:

00011010  -- 26  ( 0*128 + 0*64 + 0*32 + 1*16 + 1*8 + 0*4 + 1*2 + 1*1 )
10110110  -- 182 ( 1*128 + 0*64 + 1*32 + 1*16 + 0*8 + 1*4 + 1*2 + 0*1 )

To bitwise AND these numbers together, you AND each bit in the numbers.
ANDing two bits together gives you the output 1 if both bits are 1 and
0 otherwise. So, work your way along the two binary numbers, ANDing
each pair of bits:

00011010  -- 26
10110110  -- 182
00010010  -- AND

Note that both bits were 1 in columns 4 and 7, so bits 4 and 7 in the
result are 1, and all others are 0.

So the result of bitwise ANDing 26 and 182 together is 00010010, which
is 18.

It's called "bitwise AND" because you AND the numbers together one bit at
a time.

Does that make sense?

You can also have "bitwise OR", which works the same way, except that
two bits ORed together makes 1 if one bit or the other is 1, and it's 0
if both bits are 0. There's also XOR (also called EOR) where 
1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, and 0 XOR 0 = 0.

Aq.

-- 
Something must be Done
This is Something
Therefore, This must be Done
	-- The Thatcherite Syllogism



More information about the Wolves mailing list