Can anybody tell the internal procedure of the below code
<? $temp = 0; echo ~$temp; ?>
//instead of 1 it displays -1
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bitwise-not (~):
This inverts each bit of its operand. If the operand is a floating point value, it is truncated to an integer prior to the calculation. If the operand is between 0 and 4294967295 (0xffffffff), it will be treated as an unsigned 32-bit value. Otherwise, it is treated as a signed 64-bit value
Its because you’re actually dealing with a full 32-bit unsigned integer with NOT. What that means is you’re not simply inverting 0001 but inverting 00000000000000000000000000000001
which becomes 11111111111111111111111111111110
essentially this is the number + 1 and negated. so 1 becomes -(num+1) which is -1 or
1111111111111111111111111111110
in binary (unsigned)
for example:-
$temp=1; echo~$temp; print -2 //-(n++)