At 08:04 08.03.2003, James Taylor said:
--------------------[snip]--------------------
>Where can I read more about this?  I'm not sure that I understand why 4
>& 4 == 4.
--------------------[snip]-------------------- 

Bitwise comparison means just that, i.e. comparing two numbers bit by bit.

Any number is represented by a bit combination (this has technical reasons
- if a single "wire" has some electric potential set it's "1", if not it's
"0" (or is it the other way round? Can't remember...). A single "Byte" is
comprised of 8 such lines, each line representig a "Bit" with increasing value.

To read this correctly you need to have a monospaced font:

line number     7   6   5   4   3   2   1   0
                |   |   |   |   |   |   |   |
                |   |   |   |   |   |   |   |
line value       7   6   5   4   3   2   1   0
as power of 2:  2   2   2   2   2   2   2   2

dec. value:   128  64  32  16   8   4   2   1

Taking this, the decimal number "4" would look
    0000 0100
written in binary notation.

Ok, now let's bitwise AND 4 and 4, walking bits left to right. If both bits
are set the resulting bit is also set (for an AND operation), if one or
both bits are not set this yields a zero bit:

   0 & 0 => 0
   0 & 0 => 0
   0 & 0 => 0
   0 & 0 => 0
   0 & 0 => 0
   1 & 1 => 1
   0 & 0 => 0
   0 & 0 => 0

I hope it seems logical that the result of bitwise ANDing a number to
itself always will result in the same number...

There are a couple of bitwise operators:
  & (AND): the result contains bits set in both operands
  | (OR):  the result contains bits set in either operand
  ^ (XOR): the result contains bits set in either one or the other operand,
           but not in both (0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0, 0 ^ 0 = 0)

Unary operands:
  ~ (NOT)  reverses all bits of the single operand, e.g.;
           ~4 => 251 (~0000 0100 => 1111 1011)

Bit-Shift operators move the bits of a number to the left or right:
  <<      left shift ($a << $b means shift bits of $a $b-times to the left)
          e.g. 4 << 1 = 8 (0000 0100 << 1 = 0000 1000)
  >>      right shift ($a >> $b means shift bits of $a $b-times to the right)
          e.g. 4 >> 1 = 2 (0000 0100 >> 1 = 0000 0010)

HTH,

-- 
   >O     Ernest E. Vogelsinger
   (\)    ICQ #13394035
    ^     http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to