# IT. Expert System.

## Bitwise Operators

Bitwise operators allow evaluation and manipulation of specific bits within an integer.

Bitwise Operators
Example Name Result
`\$a & \$b` And Bits that are set in both \$a and \$b are set.
`\$a | \$b` Or (inclusive or) Bits that are set in either \$a or \$b are set.
`\$a ^ \$b` Xor (exclusive or) Bits that are set in \$a or \$b but not both are set.
`~ \$a` Not Bits that are set in \$a are not set, and vice versa.
`\$a << \$b` Shift left Shift the bits of \$a \$b steps to the left (each step means "multiply by two")
`\$a >> \$b` Shift right Shift the bits of \$a \$b steps to the right (each step means "divide by two")

Bit shifting in PHP is arithmetic. Bits shifted off either end are discarded. Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.

Use parentheses to ensure the desired precedence. For example, \$a & \$b == true evaluates the equivalency then the bitwise and; while (\$a & \$b) == true evaluates the bitwise and then the equivalency.

Be aware of data type conversions. If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.

` PHP's error_reporting ini setting uses bitwise values, providing a real-world demonstration of turning bits off. To show all errors, except for notices, the php.ini file instructions say to use: `E_ALL & ~E_NOTICE`  `

` This works by starting with E_ALL: 00000000000000000111011111111111 Then taking the value of E_NOTICE... 00000000000000000000000000001000 ... and inverting it via ~: 11111111111111111111111111110111 Finally, it uses AND (&) to find the bits turned on in both values: 00000000000000000111011111110111  `

` Another way to accomplish that is using XOR (^) to find bits that are on in only one value or the other: `E_ALL ^ E_NOTICE`  `

` error_reporting can also be used to demonstrate turning bits on. The way to show just errors and recoverable errors is: `E_ERROR | E_RECOVERABLE_ERROR`  `

` This process combines E_ERROR 00000000000000000000000000000001 and 00000000000000000001000000000000 using the OR (|) operator to get the bits turned on in either value: 00000000000000000001000000000001  `

Example #1 Bitwise AND, OR and XOR operations on integers

` <?php/* * Ignore the top section, * it is just formatting to make output clearer. */\$format = '(%1\$2d = %1\$04b) = (%2\$2d = %2\$04b)'        . ' %3\$s (%4\$2d = %4\$04b)' . "\n";echo <<<EOH ---------     ---------  -- --------- result        value      op test ---------     ---------  -- ---------EOH;/* * Here are the examples. */\$values = array(0, 1, 2, 4, 8);\$test = 1 + 4;echo "\n Bitwise AND \n";foreach (\$values as \$value) {    \$result = \$value & \$test;    printf(\$format, \$result, \$value, '&', \$test);}echo "\n Bitwise Inclusive OR \n";foreach (\$values as \$value) {    \$result = \$value | \$test;    printf(\$format, \$result, \$value, '|', \$test);}echo "\n Bitwise Exclusive OR (XOR) \n";foreach (\$values as \$value) {    \$result = \$value ^ \$test;    printf(\$format, \$result, \$value, '^', \$test);}?> `

The above example will output:

` ---------  --------- -- --------- result  value  op test ---------  --------- -- --------- Bitwise AND ( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101) ( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101) ( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101) ( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101) ( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101) Bitwise Inclusive OR ( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101) ( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101) ( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101) (13 = 1101) = ( 8 = 1000) | ( 5 = 0101) Bitwise Exclusive OR (XOR) ( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101) ( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101) ( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101) (13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101) `

Example #2 Bitwise XOR operations on strings

` <?phpecho 12 ^ 9; // Outputs '5'echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)                 // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0                        // 'a' ^ 'e' = #4echo 2 ^ "3"; // Outputs 1              // 2 ^ ((int)"3") == 1echo "2" ^ 3; // Outputs 1              // ((int)"2") ^ 3 == 1?> `

Example #3 Bit shifting on integers

` <?php/* * Here are the examples. */echo "\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n";\$val = 4;\$places = 1;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'copy of sign bit shifted into left side');\$val = 4;\$places = 2;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places);\$val = 4;\$places = 3;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'bits shift out right side');\$val = 4;\$places = 4;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'same result as above; can not shift beyond 0');echo "\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";\$val = -4;\$places = 1;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'copy of sign bit shifted into left side');\$val = -4;\$places = 2;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'bits shift out right side');\$val = -4;\$places = 3;\$res = \$val >> \$places;p(\$res, \$val, '>>', \$places, 'same result as above; can not shift beyond -1');echo "\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n";\$val = 4;\$places = 1;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places, 'zeros fill in right side');\$val = 4;\$places = (PHP_INT_SIZE * 8) - 4;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places);\$val = 4;\$places = (PHP_INT_SIZE * 8) - 3;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places, 'sign bits get shifted out');\$val = 4;\$places = (PHP_INT_SIZE * 8) - 2;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places, 'bits shift out left side');echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";\$val = -4;\$places = 1;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places, 'zeros fill in right side');\$val = -4;\$places = (PHP_INT_SIZE * 8) - 3;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places);\$val = -4;\$places = (PHP_INT_SIZE * 8) - 2;\$res = \$val << \$places;p(\$res, \$val, '<<', \$places, 'bits shift out left side, including sign bit');/* * Ignore this bottom section, * it is just formatting to make output clearer. */function p(\$res, \$val, \$op, \$places, \$note = '') {    \$format = '%0' . (PHP_INT_SIZE * 8) . "b\n";    printf("Expression: %d = %d %s %d\n", \$res, \$val, \$op, \$places);    echo " Decimal:\n";    printf("  val=%d\n", \$val);    printf("  res=%d\n", \$res);    echo " Binary:\n";    printf('  val=' . \$format, \$val);    printf('  res=' . \$format, \$res);    if (\$note) {        echo " NOTE: \$note\n";    }    echo "\n";}?> `

Output of the above example on 32 bit machines:

` --- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 4 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: same result as above; can not shift beyond 0 --- BIT SHIFT RIGHT ON NEGATIVE INTEGERS --- Expression: -2 = -4 >> 1 Decimal: val=-4 res=-2 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: same result as above; can not shift beyond -1 --- BIT SHIFT LEFT ON POSITIVE INTEGERS --- Expression: 8 = 4 << 1 Decimal: val=4 res=8 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000001000 NOTE: zeros fill in right side Expression: 1073741824 = 4 << 28 Decimal: val=4 res=1073741824 Binary: val=00000000000000000000000000000100 res=01000000000000000000000000000000 Expression: -2147483648 = 4 << 29 Decimal: val=4 res=-2147483648 Binary: val=00000000000000000000000000000100 res=10000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 30 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out left side --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- Expression: -8 = -4 << 1 Decimal: val=-4 res=-8 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111000 NOTE: zeros fill in right side Expression: -2147483648 = -4 << 29 Decimal: val=-4 res=-2147483648 Binary: val=11111111111111111111111111111100 res=10000000000000000000000000000000 Expression: 0 = -4 << 30 Decimal: val=-4 res=0 Binary: val=11111111111111111111111111111100 res=00000000000000000000000000000000 NOTE: bits shift out left side, including sign bit `

Output of the above example on 64 bit machines:

` --- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 4 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: same result as above; can not shift beyond 0 --- BIT SHIFT RIGHT ON NEGATIVE INTEGERS --- Expression: -2 = -4 >> 1 Decimal: val=-4 res=-2 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: same result as above; can not shift beyond -1 --- BIT SHIFT LEFT ON POSITIVE INTEGERS --- Expression: 8 = 4 << 1 Decimal: val=4 res=8 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000001000 NOTE: zeros fill in right side Expression: 4611686018427387904 = 4 << 60 Decimal: val=4 res=4611686018427387904 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0100000000000000000000000000000000000000000000000000000000000000 Expression: -9223372036854775808 = 4 << 61 Decimal: val=4 res=-9223372036854775808 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=1000000000000000000000000000000000000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 62 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- Expression: -8 = -4 << 1 Decimal: val=-4 res=-8 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111000 NOTE: zeros fill in right side Expression: -9223372036854775808 = -4 << 61 Decimal: val=-4 res=-9223372036854775808 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1000000000000000000000000000000000000000000000000000000000000000 Expression: 0 = -4 << 62 Decimal: val=-4 res=0 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side, including sign bit `

Warning

Don't right shift for more than 32 bits on 32 bits systems. Don't left shift in case it results to number longer than 32 bits. Use functions from the gmp extension for bitwise manipulation on numbers beyond PHP_INT_MAX.

Content

### IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 434 / 158801730. Delta: 0.03567 с