Bitwise operators?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richard.cali
Forum Newbie
Posts: 5
Joined: Fri Jun 02, 2006 11:32 pm

Bitwise operators?

Post 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
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Re: Bitwise operators?

Post 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
Last edited by PrObLeM on Tue Jun 06, 2006 3:49 pm, edited 1 time in total.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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
Last edited by TheMoose on Tue Jun 06, 2006 3:51 pm, edited 2 times in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
richard.cali
Forum Newbie
Posts: 5
Joined: Fri Jun 02, 2006 11:32 pm

Post by richard.cali »

:) thanks :)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

bitwise operators are great. really fast but easy to screw up.
Post Reply