# How does the negation ~ operator work?

7 replies to this topic

### #1 praveenhotha

praveenhotha
• Members
• Member
• 27 posts
• LocationBangalore

Posted 08 August 2006 - 05:28 AM

I was going thru the manual ,

\$a=2;
\$b=~\$a;
echo \$b;

The output was -3..  How??

### #2 corbin

corbin
• Staff Alumni
• 8,129 posts

Posted 08 August 2006 - 05:43 AM

You mean
echo \$b I'm assuming...

and I'm not positive but i think it returns the value of the -(\$a) - 1
Why doesn't anyone ever say hi, hey, or whad up world?

### #3 praveenhotha

praveenhotha
• Members
• Member
• 27 posts
• LocationBangalore

Posted 08 August 2006 - 06:06 AM

Yes its \$b, i've corrected it,
in the php manual its given this way
~ \$a Not : Bits that are set in \$a are not set, and vice versa.
What does that mean??

### #4 alecjw

alecjw
• Members
• 31 posts
• LocationHatfield, Herts, UK

Posted 08 August 2006 - 08:55 AM

Do you know binary? I think this is what it means:
If \$a was 3, for example, its binary would be:
00000011
However, PHP uses signed integers, so we add 128 to it to make 131 which would be represented like so:
10000011
Then we make all 1's 0's and vice versa.
01111100
This is 124, if we subtract 128 from this we get -4, so we know that ~13=-4

Now we can try it for 13. 13+128=141.
10001101
Reversed to:
01110010
Which is 114. 114-128=-14.

Therefore we can conclude that what corbin said is half correct. ~\$a==-(\$a)-1 is true if you're starthing with a positive number, but it's ~\$a==-(\$a)+1 if you're starting with a negative.

### #5 king arthur

king arthur
• Members
• 335 posts
• LocationUK HQ

Posted 08 August 2006 - 09:51 AM

Well not quite.

A signed 8 bit number can represent values from -128 through to +127. This is because 127 is 01111111. Adding one more makes it 10000000, which now represents -128, because it is taken to mean 128 - 256. The 8th bit is the sign bit, if it is 0 then the number is positive, if it is 1 then it's negative and represents the number - 256. So if \$a is 2, 00000010 in binary, and you take the NOT of that number (~ means logical NOT, or XOR with 11111111) you get 11111101 which is 253. 253 - 256 is -3.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

### #6 atitthaker

atitthaker
• Members
• 52 posts
• LocationBanglore, India

Posted 08 August 2006 - 10:18 AM

I am still not getting the thing. Why we are subtracting 256 from the answer we get... Can you plz make this clearer...
Beginner with PHP and need help

### #7 king arthur

king arthur
• Members
• 335 posts
• LocationUK HQ

Posted 08 August 2006 - 10:23 AM

Because an unsigned 8 bit number can store up to 255. To put it another way, 2 to the power 8 is 256.

It will work for any size integer. If we're using 16 bit numbers, then a signed 16 bit number goes from -32768 to +32767, and this time we are subtracting 65536 which is 2 to the power 16.

Basically if you understand binary you will get it, if you don't you won't.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

### #8 atitthaker

atitthaker
• Members
• 52 posts
• LocationBanglore, India

Posted 08 August 2006 - 10:35 AM

Ok. Thanks. I got the thing... Thank you very much.
Beginner with PHP and need help

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users