What does the bitwise negation in the PHP bit operation mean?

Source: Internet
Author: User
Look at the code first:

$a =8; $b =~ $a; Bitwise reverse echo "\ $a =". $a. " <br> "; echo" \ $b = ". $b;

Output Result:

$a =8$b=-9

The result is different from what I thought, because in the PHP Chinese manual it says: ~ $a not (bitwise non) sets the bit to 1 in the $a and vice versa. I simply understand that: 0 becomes 0. The original bitwise counter to the decimal output is not simply understood as such.

The first thing to know is that in computers, negative numbers are expressed in the form of a positive complement.
So we need to know three concepts, the original code, the anti-code and the complement.
Original code: An integer, converted to a binary number by the absolute size, is called the original code.
Anti-code: The binary number is reversed, the resulting new binary number is called the original binary number of the inverse code. The reverse operation means: 1 change 0, 0 to 1.
Complement: The inverse code plus 1 is called the complement. That is, to get a number of the complement, first get the anti-code, and then add the inverse code 1, the resulting number is called the complement.
The complement of positive numbers is the same as the original code, and the complement of negative numbers is the last addition of 1

by the first known, 8 bitwise negation in decimal output process:
1, 8 of the original code is: 0000 0000 0000 0000 0000 0000 0000,
2, Bitwise negation: 1111 1111 1111 1111 1111 1111 1111 0111; Because the first digit is 1, it is negative. That is, the bitwise negation is negative
the following decimal output: Negative numbers are expressed in the complement of their positive values:
3, 2 steps of the number (it is to take the inverse, the symbol is unchanged, the highest bit or 1) counter: 0000 0000 0000 0000 0000 4, anti-code plus 1:1000 0000 0000 0000 0000 0000 000 1001; This will get a positive complement of negative numbers. That's--9.

Give me another example. If 2 is reversed by bitwise.
0000 0000 0000 0000 0000 0000 0000 0010 (Original code)
1111 1111 1111 1111 1111 1111 1111 1101 (bitwise reverse)
1000 0000 0000 0000 00 00 0000 000 0010 (positive inverse, symbol unchanged)
1000 0000 0000 0000 0000 0000 000 0011 (plus 1, result-3)
The code and results are as follows:

<?php$a=2; $b =~ $a;  Bitwise reverse echo "\ $a =". $a. " <br> "; echo" \ $b = ". $b;? >

Results:

$a =2$b=-3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.