Page 1 of 1

Bitwise operators?

Posted: Tue Jun 06, 2006 3:42 pm
by richard.cali
Hi
Try to start learning PHP, with Welling book....my programmig experience is very poor.
I dont understand bitwise XOR...
why 4^12= output 8
12^9= output 5

Tried to find answer :( , Could anybody explain this, please!
thank you

Re: Bitwise operators?

Posted: Tue Jun 06, 2006 3:48 pm
by PrObLeM
Because it works on the bits

About Xor: http://en.wikipedia.org/wiki/Xor

basically
0 and 0 -> 0
0 and 1 -> 1
1 and 0 -> 1
1 and 1 -> 0

Code: Select all

4^12= output 8

00000100   <- binary representation of 4
00001100   <- binary representation of 12
-------------
00001000   <- binary representation of 8

Code: Select all

12^9= output 5

00001100   <- binary representation of 12
00001001   <- binary representation of 9
------------
00000101   <- binary representation of 5

Posted: Tue Jun 06, 2006 3:49 pm
by TheMoose
The bitwise XOR takes the binary version of the numbers and compares each the bits of each. If both are 0, they stay 0. If both are 1, they turn to 0. If they are opposite, they turn to 1.

In this case,

Code: Select all

4  = 0100
12 = 1100
8  = 1000

12 = 1100
9  = 1001
5  = 0101

Posted: Tue Jun 06, 2006 3:50 pm
by shiznatix
a good tutorial by feyd in this thread:

viewtopic.php?t=44460&highlight=bitwise

maybe that should be put in the tutorials section?

Posted: Tue Jun 06, 2006 4:03 pm
by richard.cali
:) thanks :)

Posted: Tue Jun 06, 2006 4:18 pm
by MrPotatoes
bitwise operators are great. really fast but easy to screw up.