note 105821 added to language.operators.bitwise
| From: | spencer-p-moy at example dot com | Date: | Sun, 18 Sep 2011 01:21:14 +0000 |
| Subject: | note 105821 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183237@lists.php.net to get a copy of this message | ||
The NOT or complement operator ( ~ ) and negative binary numbers can be confusing.
~2 = -3 because you use the formula ~x = -x - 1 The bitwise complement of a decimal number is the
negation of the number minus 1.
NOTE: just using 4 bits here for the examples below but in reality PHP uses 32 bits.
Converting a negative decimal number (ie: -3) into binary takes 3 steps:
1) convert the positive version of the decimal number into binary (ie: 3 = 0011)
2) flips the bits (ie: 0011 becomes 1100)
3) add 1 (ie: 1100 + 0001 = 1101)
You might be wondering how does 1101 = -3. Well PHP uses the method "2's complement"
to render negative binary numbers. If the left most bit is a 1 then the binary number is negative
and you flip the bits and add 1. If it is 0 then it is positive and you don't have to do
anything. So 0010 would be a positive 2. If it is 1101, it is negative and you flip the bits to get
0010. Add 1 and you get 0011 which equals -3.
----
Server IP: 69.147.83.197
Probable Submitter: 68.227.80.11
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/105821
Del: integrated -- https://master.php.net/note/delete/105821/integrated
Del: useless -- https://master.php.net/note/delete/105821/useless
Del: bad code -- https://master.php.net/note/delete/105821/bad+code
Del: spam -- https://master.php.net/note/delete/105821/spam
Del: non-english -- https://master.php.net/note/delete/105821/non-english
Del: in docs -- https://master.php.net/note/delete/105821/in+docs
Del: other reasons-- https://master.php.net/note/delete/105821
Reject -- https://master.php.net/note/reject/105821
Search -- https://master.php.net/manage/user-notes.php