Archive - Mar 10, 2007 - Blog entry

Date

Explore bitwise operator properties

These are the bitwise operators in PHP :
&: AND
|: OR
^: XOR
~: NOT
<<: Left Shift
>>: Right Shift
Please read the PHP Bitwise Tutorial by Jim Plush if you have problem understanding bitwise operators
Here I will be only talking about the first 3 operators.
I quite like math, that’s why I want to know what properties these operators has. I worked for the entire afternoon then I found there is a Table of Logic with some of the things I worked on.

Let’s define 4 binary numbers.
a and b:any binary number
m and M: m is a binary number with all zeros(ex. 0000000), M is a binary number with all ones(ex. 1111111).

This table show some bitwise identities.
Operator a operator a a operator ~a a operator m a operator M
& a m m a
| a M a M
^ m M a ~a

The three binary bitwise operators(AND, OR, XOR) have commutative property, so:
a^b = b^a
a|b = b|a
a&b = b&a

Associative Property:
a^b^c = a^(b^c)
a|b|c = a|(b|c)
a&b&c = a&(b&c)

Distributive Property
a&(b^c) = (a&b) ^ (a&c)
a&(b|c) = (a&b) | (a&c)
a|(b&c) = (a|b)&(a|c)

The table show XOR have a special property, when XOR is used against itself, it result zero. Because of the associative property and the commutative property, XOR usually implemented in some encryption system
a^b=c
c^a=b
Suppose a is the key and b is the text, XOR them(encrypt) and you get c. c can be convert back(decrypt) into b by XOR it with a, which is usually only known to the user.

There are also things I found out during the test, just click the table header and you will see some equivalence that apply to bitwise operators. Each time you refresh the page, the table should change. $a and $b are assigned with rand(0,16777216)
Expression Result
$a10110111101101111110101
$b110010111001010110100111
$a^$b100100000100111001010010
$a&$b10010111001000110100101
$a|$b110110111101111111110111
$a^~$b1111111111111111111111111111111111111111011011111011000110101101
$a&~$b100000100101001010000
$a|~$b1111111111111111111111111111111111111111011111111111101111111101
~$a^~$b100100000100111001010010
~$a&~$b1111111111111111111111111111111111111111001001000010000000001000
~$a|~$b1111111111111111111111111111111111111111101101000110111001011010
$a^$b|$a110110111101111111110111
$a|$b^$a110110111101111111110111
$a^$b&$a100000100101001010000
$a&$b^$a100000100101001010000
$a|$b&$a10110111101101111110101
$a&$b|$a10110111101101111110101
~$a^$b|$a1111111111111111111111111111111111111111011111111111101111111101
~$a|$b^$a1111111111111111111111111111111111111111101101000110111001011010
~$a^$b&$a1111111111111111111111111111111111111111111011111011010110101111
~$a&$b^$a110110111101111111110111
~$a|$b&$a1111111111111111111111111111111111111111111011111011010110101111
~$a&$b|$a110110111101111111110111
~$a|$b^~$a1111111111111111111111111111111111111111111011111011010110101111
~$a^$b|~$a1111111111111111111111111111111111111111111011111011010110101111
~$a^$b&~$a1111111111111111111111111111111111111111001001000010000000001000
~$a&$b^~$a1111111111111111111111111111111111111111001001000010000000001000
~$a|$b&~$a1111111111111111111111111111111111111111101001000010010000001010
~$a&$b|~$a1111111111111111111111111111111111111111101001000010010000001010
~$a|~$b^~$a1111111111111111111111111111111111111111101101000110111001011010
~$a^~$b|~$a1111111111111111111111111111111111111111101101000110111001011010
~$a^~$b&~$a100000000000010000000010
~$a&~$b^~$a100000000000010000000010
~$a|~$b&~$a1111111111111111111111111111111111111111101001000010010000001010
~$a&~$b|~$a1111111111111111111111111111111111111111101001000010010000001010
$a|~$b^~$a110110111101111111110111
$a^~$b|~$a1111111111111111111111111111111111111111111011111011010110101111
$a^~$b&~$a1111111111111111111111111111111111111111011111111111101111111101
$a&~$b^~$a1111111111111111111111111111111111111111101101000110111001011010
$a|~$b&~$a1111111111111111111111111111111111111111011111111111101111111101
$a&~$b|~$a1111111111111111111111111111111111111111101101000110111001011010
With the table above, some bitwise operations can be optimized. For example: (a|~b^~a)^(a^b|a) can be simplified as 0
Honey Pot that kill bots