php bitwise operator

1. Bitwise AND (&): Convert a number to binary. and then operate. Align its digits. If the same bits are all 1. is 1. Otherwise it is 0; example:

$a = 5;  0101;                               $a = 35;  10 0011;

$b = 9 ;   1001;                               $b = 23;  01 0111;

$a & $b = 0001; decimal 1 $a & $b = 00 0011; decimal 3

2. Bitwise OR (|): Convert a number to binary. then perform an OR operation. Align its digits. If the same bit is not 1, it is 0. 1 otherwise; example:

$a = 5;  0101;                                $a = 35;  10 0011;

$b = 9 ;   1001;                                 $b = 23; 01 0111;

$a & $b = 1101; decimal is 13 $a & $b = 110111; decimal is 55

3. Bitwise NOT (~): First make it clear. Computers only know two's complement. The positive complement code is the same as the original code. The complement of a negative number is the one's complement plus one. ~ is the inversion (1 becomes 0, 0 becomes 1);

Take negative 5 as an example:

Positive number 5 (32nd place): 00000000 00000000 00000000 00000101

Inverse code: 11111111 11111111 11111111 11111010

-5's complement: 11111111 11111111 11111111 11111011


Positive number 5 (32nd place): 00000000 00000000 00000000 00000101

Inverse code: 11111111 11111111 11111111 11111010

5's complement: 00000000 00000000 00000000 00000101

Example: $a = 5; $c = ~$a;

 5's complement for ~ (to get the complement of a negative number) 111111111111111111111111111111010

 Negative complement restore to one's complement (-1): 111111111111111111111111111111001

  Get the original code of negative number: 000000000000000000000000000000110

  Output: 6 false. A positive number ~ is a negative number; so it is -6;

The following uses -5 as an example: -5's complement 11111111 11111111 11111111 11111011

                               ~       00000000 00000000 00000000 00000100

                                Inverse 11111111 11111111 11111111 11111011

                                Original code 00000000 00000000 00000000 00000100

 output: 4

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325868972&siteId=291194637