Last night in reading PHP books, saw an operator "~" Explanation is the bitwise take the inverse operator, because usually used not much so it is not very familiar with it, so I took a bit on the net, feel not particularly clear, so I spent a bit of time to study a bit, finally a little results, so here for everyone to share , if it is wrong, please criticize the point. Well, here's the bitwise negation.
Operators are first divided into operands, such as: $a > $b, $a > $b? 1:0. The first expression in the operand is two $ A and $b, so this is called the two-dollar operation, according to this, the expression two is ternary. Therefore, bitwise inversion is a unary operator such as: $a =~ $b;
Second, we want to understand that the data stored in the computer operation is 0,1 this binary data. and is stored in the form of complementary source code, the following leads to three concepts:
Source code: An integer, converted to binary number by absolute size, the highest bit is reserved for the sign bit. The highest bit 0 represents a positive number, and 1 indicates a negative number, such as ( -3:1000 0000 0000 0000 0000 0000 0000 0011).
Anti-code: is the source of the reversal, the highest sign of the exception, the remaining 1 change 0,0 1.
Complement: That is, the data stored by the computer, defined as the code of the Anti-code plus 1. (Note: A positive complement is the source code itself)
Here is another thing to explain, just in the source of the time to see the 3 this number accounted for 32, we are thinking about why 32 is not the other one. In fact, we can learn about PHP5. The maximum number of integers in the X version is -2e31 to +2e31
There is a sign bit, so the highest value is 31 power. In the high version of PHP will be reached 64-bit is -2e63 to +2e63, because now use more than the version is 5. Version of X so it is in 32-bit binary terms.
Well, now that we understand the concept, let's take a look at the specific operation:
First look at the results of the code operation
<?php $a =-3; $b = ~ $a; Var_dump ($b); int 2?>
The above is-3 bitwise reversed to get 2, we use the above theory to analyze:
-3 Source: 1000 0000 0000 0000 0000 0000 0000 0011
Anti-code: 1111 1111 1111 1111 1111 1111 1111 1100
Complement: 1111 1111 1111 1111 1111 1111 1111 1101 (anti-code plus 1 get complement)
Now, after performing a bitwise-Reverse operation:
Complement: 0000 0000 0000 0000 0000 0000 0000 0010
Anti-code: 0000 0000 0000 0000 0000 0000 0000 0010
Source: 0000 0000 0000 0000 0000 0000 0000 0010
Why do the bitwise take reverse after the source code and the same complement? Well, in fact, the above has been explained, in the source code for the conversion of the symbol bit is unchanged. We see that the post-bitwise counter-complement sign bit is 0 for positive numbers, and the complement of positive numbers is the source code itself. So the source code decimal conversion is 2, I think this is clear! Do you understand now? < Thank you for your support, growth from the beginning of the blog >
This article is from the "Grow from Blog" blog, be sure to keep this source http://lxqybyq.blog.51cto.com/7868338/1683061
The understanding of bitwise inversion in PHP