Reply to Re: Whats the diffecence between '&' and '&&' operators?

Your name:

Reply:


Posted by Kimmo Laine on 05/25/05 19:01

"Jam Pa" <anonanon@non-anon.org> kirjoitti
viestissδ:Xns9661B5FF25BABanonanonnonanonorgjp@213.243.153.2...
> Both '&' and '&&' are 'AND' right? Or what about 'ORs' '|' and '||'?
>
> Whats the difference? I not grok....

& and | are bitwise arithmetic operators while && and || are boolean
operators. What this maeans is that & and | handle single bits in and
integer, but && and || evaluate the entire integer as true or false.

For example we have and integer 4. when it is splitted in bits it looks like
0100
( = 0*(2^3) + 1*(2^2) + 0*(2^1) + 0*(2^0) )
And we have integer 8 that's 1000
( = 1*(2^3) + 0*(2^2) + 0*(2^1) + 0*(2^0) )

Now, comparing them (8 && 4) this returns TRUE, since both are none-zero
(there is at least one bit other than zero in both of them.

Bitwise operation on the other hand: 8 & 4 will return zero. This is because
it compares each single bit:
8 & 4 = 0
-----------
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
0 and 0 = 0

Now OR then. 4 || 8 returns TRUE, since within the two operators there is at
least one none-zero bit. Bitwise operation 8 | 4 returns 12. Let's see that
again in bitwise:

8 | 4 = 12
-----------
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
0 or 0 = 0

because 1100 is the binary representation of 12. It's not the same as 8 + 4,
that was just a coincidence. 8 | 8 returns 8. :)

8 | 8 = 8
-----------
1 or 1 = 1
0 or 0 = 0
0 or 0 = 0
0 or 0 = 0

See the difference? When using && and || we just look if the integer is zero
or non-zero, but using & and | (and xor ^) we perform bitwise arithmetic.

AND and OR are not the same as && and ||, but they are closer to them, than
to & and |. I never understood completely the difference, but there is a
section in the manual that trys to explain it, but I just can't find it now.
All I found was this: "The reason for the two different variations of "and"
and "or" operators is that they operate at different precedences. " (PHP
Manual). Operator precendence is all about in what order the expression is
evaluated.

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

eternal.erectionN0@5P4Mgmail.com

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация