praveenhotha Posted August 8, 2006 Share Posted August 8, 2006 I was going thru the manual , $a=2; $b=~$a; echo $b;The output was -3.. How?? Quote Link to comment Share on other sites More sharing options...
corbin Posted August 8, 2006 Share Posted August 8, 2006 You meanecho $b I'm assuming... and I'm not positive but i think it returns the value of the -($a) - 1 Quote Link to comment Share on other sites More sharing options...
praveenhotha Posted August 8, 2006 Author Share Posted August 8, 2006 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?? Quote Link to comment Share on other sites More sharing options...
alecjw Posted August 8, 2006 Share Posted August 8, 2006 Do you know binary? I think this is what it means:If $a was 3, for example, its binary would be:00000011However, PHP uses signed integers, so we add 128 to it to make 131 which would be represented like so:10000011Then we make all 1's 0's and vice versa.01111100This is 124, if we subtract 128 from this we get -4, so we know that ~13=-4Now we can try it for 13. 13+128=141.10001101Reversed to:01110010Which 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. Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 8, 2006 Share Posted August 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
atitthaker Posted August 8, 2006 Share Posted August 8, 2006 I am still not getting the thing. Why we are subtracting 256 from the answer we get... Can you plz make this clearer... Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 8, 2006 Share Posted August 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
atitthaker Posted August 8, 2006 Share Posted August 8, 2006 Ok. Thanks. I got the thing... Thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.