Hi all, let's continue with our operators. The next in the series is Bitwise operators:
Note: Please go through
part 1 and
part 2 of php operators here.
Bitwise operators:
Bitwise operators works on the binary representation of integers. Means first the integer operand will be converted to its binary form and then performs its operation based on the operator.
Note: The binary form means zero's(0) and one's(1).
The bitwise operators are as follows:
& (AND), | (OR), ^ (XOR), ~ (NOT), << (Shift Left), >> (Shift Right)
Bitwise AND (&) :
If both the bits are set, means both the binary values are 1 then the output will be 1 else 0. Here the bit set means 1 and bit not set means 0. Let's see a small demo on bitwise AND (&).
Demo:
Let's take two numbers 46 and 93 respectively. Now perform binary conversion for these two numbers.
Step 1:
Converting decimal numbers 46 and 93 into their binary form.
The binary representation of 46 - 00101110
The binary representation of 93 - 01011101
I guess you are little bit confused with the binary form. How the decimal numbers got translated to binary form. You are not alone, even i too confused with these binary form. After recollecting my engineering subjects i got clear solution. See the below tables about how to convert decimal number to binary conversion?
A decimal number 46 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
0 |
32 |
0 |
8 |
4 |
2 |
0 |
= |
46 |
A decimal number 93 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
64 |
0 |
16 |
8 |
4 |
0 |
1 |
= |
93 |
Step 2:
Now perform Bitwise AND Operation on them. The output will be 00001100.
After performing AND operation on both the binary numbers, the bit set occurs at 5th digit and 6th digit in both the binary forms. So the 5th digit is at 23 and 6th digit is at 22 position. Now you may get confusion that how to perform the Bitwise Operation on those numbers. Simply perform the AND operation then you will get the result. See below that how to perform bitwise operation on those two numbers.
|
Base 2 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
Binary 46 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
Binary 93 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
Result |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
0 |
|
|
Now from the above manipulation, the bit set occurs 5th and 6th position.
Step 3:
Now perform addition operation on these two 2
3 and 6th digit is at 2
2. You will get 2
3 + 2
2 = 12.
Now you may be confused that how this answer 12 came. Don't get confused. Simply multiply the binary number quotient with base 2 and then perform addition on those numbers then you will get the answer.
| (27 x 0) |
+ |
(26 x 0) |
+ |
(25 x 0) |
+ |
(24 x 0) |
+ |
(23 x 1) |
+ |
(22 x 1) |
+ |
(21 x 0) |
+ |
(20 x 0) |
= 12 |
Huh...These many steps...its really very frustrating..Don't panic..you don't have to worry about these many steps. Let's see the same in our php code. Its really very simple.
$x=46;
$y=93;
echo $x & $y; // output: 12
Bitwise OR (|) :
If any one of the bit is set, means any one of the binary value is 1 then the output will be 1 else 0. Here the bit set means 1 and bit not set means 0. Let's see a small demo on bitwise OR (|).
Demo:
Let's take two numbers 46 and 93 respectively. Now perform binary conversion for these two numbers.
Step 1:
Converting decimal numbers 46 and 93 into their binary form.
The binary representation of 46 - 00101110
The binary representation of 93 - 01011101
I guess you are little bit confused with the binary form. How the decimal numbers got translated to binary form. You are not alone, even i too confused with these binary form. After recollecting my engineering subjects i got clear solution. See the below tables about how to convert decimal number to binary conversion?
A decimal number 46 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
0 |
32 |
0 |
8 |
4 |
2 |
0 |
= |
46 |
A decimal number 93 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
64 |
0 |
16 |
8 |
4 |
0 |
1 |
= |
93 |
Step 2:
Now perform Bitwise OR Operation on them. The output will be 01111111.
After performing OR operation on both the binary numbers, the bit set occurs at 2nd, 3rd, 4th, 5th, 6th, 7th and 8th digits respectively in both the binary forms. So these digits are at 26,25,24,23,22,21,and at 20 positions. Now you may get confusion that how to perform the Bitwise Operation on those numbers. Simply perform the OR operation then you will get the result. See below that how to perform bitwise operation on those two numbers.
|
Base 2 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
Binary 46 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
Binary 93 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
Result |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
|
|
Now from the above manipulation, the bit set occurs at 2nd, 3rd, 4th, 5th, 6th, 7th, and 8th positions respectively.
Step 3:
Now perform addition operation on these 2
6,2
5,2
4,2
3,2
2,2
1,and at 2
0 positions. You will get = 127.
Now you may be confused that how this answer 127 came. Don't get confused. Simply multiply the binary number quotient with base 2 and then perform addition on those numbers then you will get the answer.
| (27 x 0) |
+ |
(26 x 1) |
+ |
(25 x 1) |
+ |
(24 x 1) |
+ |
(23 x 1) |
+ |
(22 x 1) |
+ |
(21 x 1) |
+ |
(20 x 1) |
= 127 |
Huh...These many steps...its really very frustrating..Don't panic..you don't have to worry about these many steps. Let's see the same in our php code. Its really very simple.
$x=46;
$y=93;
echo $x | $y; // output: 127
Bitwise XOR (^) :
If any one of the bit is set, means any of the binary value is 1 then the output will be 1. If both the bits are either 1 or 0 then output will be 0.
Here the bit set means 1 and bit not set means 0. Let's see a small demo on bitwise XOR (^).
Demo:
Let's take two numbers 46 and 93 respectively. Now perform binary conversion for these two numbers.
Step 1:
Converting decimal numbers 46 and 93 into their binary form.
The binary representation of 46 - 00101110
The binary representation of 93 - 01011101
A decimal number 46 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
0 |
32 |
0 |
8 |
4 |
2 |
0 |
= |
46 |
A decimal number 93 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
64 |
0 |
16 |
8 |
4 |
0 |
1 |
= |
93 |
Step 2:
Now perform Bitwise XOR Operation on them. The output will be 01110011.
After performing XOR operation on both the binary numbers, the bit set occurs at 2nd, 3rd, 4th, 7th and 8th digits respectively. So the 2nd, 3rd, 4th, 7th and 8th is at 2
6, 2
5, 2
4, 2
1 and 2
0 positions. See below that how to perform XOR bitwise operation on those two numbers.
|
Base 2 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
Binary 46 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
Binary 93 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
Result |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
|
|
Now from the above manipulation, the bit set occurs at 2nd, 3rd, 4th, 7th and 8th digits respectively.
Step 3:
Now perform addition operation on these 2
6, 2
5, 2
4, 2
1 and 2
0. You will get 2
6 + 2
5 + 2
4 + 2
1 + 2
0 = 115.
Now you may be confused that how this answer 115 came. Don't get confused. Simply multiply the binary number quotient with base 2 and then perform addition on those numbers then you will get the answer.
| (27 x 0) |
+ |
(26 x 1) |
+ |
(25 x 1) |
+ |
(24 x 1) |
+ |
(23 x 0) |
+ |
(22 x 0) |
+ |
(21 x 1) |
+ |
(20 x 1) |
= 115 |
Huh...These many steps...its really very frustrating..Don't panic..you don't have to worry about these many steps. Let's see the same in our php code. Its really very simple.
$x=46;
$y=93;
echo $x ^ $y; // output: 115
Bitwise NOT(~) :
The Bitwise NOT operator perform on two values and returns true when set bit of one expression is not set in another expression.
Demo:
Let's take two numbers 46 and 93 respectively. Now perform binary conversion for these two numbers.
Step 1:
Converting decimal numbers 46 and 93 into their binary form.
The binary representation of 46 - 00101110
The binary representation of 93 - 01011101
A decimal number 46 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
0 |
32 |
0 |
8 |
4 |
2 |
0 |
= |
46 |
A decimal number 93 can be represent in binary form like bellow -
| 1 Byte ( 8 bits ) |
| Decimal Place |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
|
|
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
0 |
64 |
0 |
16 |
8 |
4 |
0 |
1 |
= |
93 |
Step 2:
Now perform Bitwise NOT Operation on them. The output will be 00100010.
Now from the above manipulation, the bit set occurs at 3rd, and 7th digits respectively.
Step 3:
Now perform addition operation on these, You will get 25 + 21 = 34.
Now you may be confused that how this answer 34 came. Don't get confused. Simply multiply the binary number quotient with base 2 and then perform addition on those numbers then you will get the answer.
|
Base 2 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
|
|
Binary 46 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
|
|
|
Binary 93 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
|
|
|
Result |
0 |
0 |
1 |
0 |
0 |
0 |
1 |
0 |
| (27 x 0) |
+ |
(26 x 0) |
+ |
(25 x 1) |
+ |
(24 x 0) |
+ |
(23 x 0) |
+ |
(22 x 0) |
+ |
(21 x 1) |
+ |
(20 x 0) |
= 34 |
Huh...These many steps...its really very frustrating..Don't panic..you don't have to worry about these many steps. Let's see the same in our php code. Its really very simple.
$x=46;
$y=93;
echo $x &^ $y; // output: 34
Left and Right Shift:
If a and b are two numbers, BIT SHIFTING shifts a bits b number of steps. each step refers to multiply by two if it is BIT SHIFT LEFT. If it is BIT SHIFT RIGHT, then each step refers to division by two.
Demo:
$x=8;
$y=3;
echo $x >> $y; // output: 1
echo $x << $y; // output: 64