PHP About bitwise operators

Source: Internet
Author: User
Common four bit Operators:& (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise inverse)

& (Bitwise AND): The corresponding bit is 1 o'clock at the same time, 1 after the & operation, otherwise 0

| (bitwise OR): one of the corresponding bits is 1 o'clock, and after the | operation is 1, and 0 is 0

^ (Bitwise XOR): The corresponding bit is 1 o'clock, the ^ operation is 1, at the same time 0 is 0, and the 1 o'clock ^ operation is also 0

~ (bitwise Reversed): $a + (~ $a) =-1

In a computer, negative numbers are expressed in the form of a positive complement.
Here we need to know three concepts, original code, anti-code and complement.

The original code: an integer, converted into a binary number according to the absolute size, called the original code.

8 The original code is: 0000 0000 0000 0000 0000 0000 0000 1000

Inverse 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.

8 bitwise inversion: 1111 1111 1111 1111 1111 1111 1111 0111

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 a positive number is the same as its original code; the complement of a negative number is the minus 1 of its inverse code.

Anti-code plus 1:1000 0000 0000 0000 0000 0000 000 1001; This will get a positive complement of negative numbers. That's--9.

Reference: http://www.cnblogs.com/setsail/archive/2013/05/22/3093542.html

http://my.oschina.net/WarRome/blog/90707

The above explanation oneself also did not see too understand, but practice obtains $a+ (~ $a) =-1

Here are a few examples:

$m = 8;
$n = 12;
$p =-109;
$mn = $m & $n;
echo $mn. "
";
$mn = $m | $n;
echo $mn. "
";
$mn = $m ^ $n;
echo $mn. "
";
$mn =~ $m;
echo $mn. "
";
$mn =~ $p;
echo $mn. "
";
?>

Output Result:

8
12
4
-9
108

On the issue of bitwise inversion, and then pondering

  • 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.