PHP Tutorial Tutorials - PHP Bitwise Operators

PHP Bitwise Operators

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

Operator Name Description Example Result Bitwise
$a & $b And Bits that are set in both $a and $b are set. 4 & 5 4 0100 & 0101 = 0100
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set. 4 |5 5 0100 | 0101 = 0101
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set. 4 ^ 5 4 0100 ^ 0101 = 0001
~ $a Not Bits that are set in $a are not set, and vice versa. ~ 4 -5 -
$a << $b Shift left Shift the bits of $a $b steps to the left 4 << 5 128 X=0
0100
-----------
0100XXXXX
$a >> $b Shift right Shift the bits of $a $b steps to the right 4 >> 5 0 X=0
0100
-----------
XXXXX

Bitwise Not operations on integers

How to store the negative integer in the Computer?
Assume we have a 8-bit integer 5. It's bits are:
Bit code: 00000101
Inverse code: 11111010
Complement code: 11111011 (Inverse code + 1)
So for the negative integer -5, It's bits code = Complement code: 11111011

It's the same for 32-bit integer.
-5 = 11111111111111111111111111111011

<?php
$a = 4;
$b = ~$a;
$format = '%1$2d = %1$04b';
printf($format,$b);
?>

Output

-5 = 1111111111111111111111111111111111111111111111111111111111111011

64-bit system:

d:\php-7.1.32-Win32-VC14-x64>php -v
PHP 7.1.32 (cli) (built: Aug 28 2019 09:09:30) ( ZTS MSVC14 (Visual C++ 2015) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

Bit shifting on integers

Right shift on 32-bit system:

4 = 0100
      4 = S0000000000000000000000000000100 (S is the sign)
4 >> 1: SS000000000000000000000000000010 (Right shifts) Result:2
4 >> 2: SSS00000000000000000000000000001 (Right shifts) Result:1
Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.

<?php
$a = 4;
$b = -4;
$format = '%1$6s = %2$64b = %2$4d ';

printf($format,"$a",$a);
echo PHP_EOL;
printf($format,"$a>>1",$a>>1);
echo PHP_EOL;
printf($format,"$a>>2",$a>>2);
echo PHP_EOL;
printf($format,"$a>>29",$a>>29);
echo PHP_EOL;
printf($format,"$a>>30",$a>>30);
echo PHP_EOL;
echo PHP_EOL;

printf($format,"$b",$b);
echo PHP_EOL;
printf($format,"$b>>1",$b>>1);
echo PHP_EOL;
printf($format,"$b>>2",$b>>2);
echo PHP_EOL;
printf($format,"$b>>29",$b>>29);
echo PHP_EOL;
printf($format,"$b>>30",$b>>30);
echo PHP_EOL;
?>

Output:(64-bit system)

     4 =                                                              100 =    4 
  4>>1 =                                                               10 =    2 
  4>>2 =                                                                1 =    1 
 4>>29 =                                                                0 =    0 
 4>>30 =                                                                0 =    0 

    -4 = 1111111111111111111111111111111111111111111111111111111111111100 =   -4 
 -4>>1 = 1111111111111111111111111111111111111111111111111111111111111110 =   -2 
 -4>>2 = 1111111111111111111111111111111111111111111111111111111111111111 =   -1 
-4>>29 = 1111111111111111111111111111111111111111111111111111111111111111 =   -1 
-4>>30 = 1111111111111111111111111111111111111111111111111111111111111111 =   -1 

Left shift on 32-bit system:

4 = 0100
      4 = S0000000000000000000000000000100 (S is the sign)
4 << 1: 0000000000000000000000000000100X (Left shifts ,X = 0) Result:8
4 << 2: 000000000000000000000000000100XX (Left shifts ,X = 0) Result:16
4 <<29: 100XXXXXXXXXXXXXXXXXXXXXXXXXXXXX (Left shifts ,X = 0) Result:-2147483648
Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left.

<?php
$a = 4;
$b = -4;
$format = '%1$6s = %2$64b = %2$4d ';

printf($format,"$a",$a);
echo PHP_EOL;
printf($format,"$a<<1",$a<<1);
echo PHP_EOL;
printf($format,"$a<<2",$a<<2);
echo PHP_EOL;
printf($format,"$a<<29",$a<<29);
echo PHP_EOL;
printf($format,"$a<<30",$a<<30);
echo PHP_EOL;
echo PHP_EOL;

printf($format,"$b",$b);
echo PHP_EOL;
printf($format,"$b<<1",$b<<1);
echo PHP_EOL;
printf($format,"$b<<2",$b<<2);
echo PHP_EOL;
printf($format,"$b<<29",$b<<29);
echo PHP_EOL;
printf($format,"$b<<30",$b<<30);
echo PHP_EOL;
?>

Output:(64-bit system)

     4 =                                                              100 =    4 
  4<<1 =                                                             1000 =    8 
  4<<2 =                                                            10000 =   16 
 4<<29 =                                 10000000000000000000000000000000 = 2147483648 
 4<<30 =                                100000000000000000000000000000000 = 4294967296 

    -4 = 1111111111111111111111111111111111111111111111111111111111111100 =   -4 
 -4<<1 = 1111111111111111111111111111111111111111111111111111111111111000 =   -8 
 -4<<2 = 1111111111111111111111111111111111111111111111111111111111110000 =  -16 
-4<<29 = 1111111111111111111111111111111110000000000000000000000000000000 = -2147483648 
-4<<30 = 1111111111111111111111111111111100000000000000000000000000000000 = -4294967296 

Notes: if you're using PHP 5.x, don't shift more than 31 bits on a 32-bit system

Date:2019-10-01 01:49:07 From:www.Lautturi.com author:Lautturi