Bitwise operators allow operating on the bitwise representation of their arguments.
Operator | Name | Example | Result |
---|---|---|---|
& | And | $x & $y | Bits that are set in both $x and $y are set. |
| | Or | $x | $y | Bits that are set in either $x or $y are set. |
^ | Xor | $x ^ $y | Bits that are set in $x or $y but not both are set. |
~ | Not | ~$x | Bits that are set in $x are not set, and vice versa. |
<< | Shift left | $x << $y | Shift the bits of $x $y steps to the left.# |
>> | Shift right | $x >> $y | Shift the bits of $x $y steps to the right.* |
# Each step means 'multiply by two'
* Each step means 'divide by two'
What is a bit?
A bit (Binary digIT) is the basic unit of information stored in the computing system that exists in two possible states, represented as ON or OFF. In a computer system, the ON state considered as 1 and OFF state considered as 0. These states can be compared with two states of a flip-flop, two states of an electric switch ( ON and OFF) e.t.c. These two values 0 and 1 are called Binary digit and these digits are in a specific number system, that is BINARY number system which constructs upon the base of 2.
In decimal number system, a number construct upon the base of 10. Let us see how a decimal number can be constructed -
231=(2 x 10^{2})+(3 x 10^{1})+(1 x 10^{0})
=200+30+1
=231
The binary number system also follows the same concept. The only difference is the base is 2 instead of 10. Let us see how a binary number can be converted into a decimal number -
1011010=(1 x 2^{6})+(0 x 2^{5})+(1 x 2^{4})+(1 x 2^{3})+(0 x 2^{2})+(1 x 2^{1})+(0 x 2^{0})
=(1 x 64) +(0 x 32)+(1 x 16)+(1 x 8)+(0 x 4)+(1 x 2)+(0 x 1)
=64+0+16+8+0+2+0
=90
So, (1011010)_{2}= (90)_{10}
Byte
A byte is made up of a sequence of bits. A byte consists of eight bits. The maximum value of a byte is 255, so the place value of each bit is set.
Tabular representation of a byte
1 Byte ( 8 bits ) | ||||||||
---|---|---|---|---|---|---|---|---|
8th | 7th | 6th | 5th | 4th | 3rd | 2nd | 1st | |
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Here is a Tabular representation of a byte shows how the maximum value of a byte is 255
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |||
2^{7} | 2^{6} | 2^{5} | 2^{4} | 2^{3} | 2^{2} | 2^{1} | 2^{0} | |||
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | = | 255 |
A decimal number 93 can be represent in binary form like bellow -
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
0 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | |||
2^{7} | 2^{6} | 2^{5} | 2^{4} | 2^{3} | 2^{2} | 2^{1} | 2^{0} | |||
0 | 64 | 0 | 16 | 8 | 4 | 0 | 1 | = | 93 |
Bitwise AND
Example of PHP bitwise AND with one shared bit
<?php $x=13; $y=22; echo $x & $y; ?>
Output of the example
4
Explanation
So referring from our tables above it can be said that the only bit this two share is in the 3rd position on place value 4. So $x & $y = 4.
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | = | 13 |
$y | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | = | 22 |
In the above table, the value set for $x(13) in the 1st, 3rd and 4th place. The place value respectively are 1,4 and 8 and the value set for $y(22) in the 2nd, 3rd and 5th place with the corresponding place value 2, 4 and 16.
So you can see from the above table that, the only bit the $x and $y shared together is the 3rd bit. So 4 is returned
View the example of php bitwise AND with one shared bit in the browser
Lets look at another example of the & operator which share more bits -
Example of PHP bitwise AND with two shared bit
<?php $x=77; $y=198; echo $x & $y; ?>
Output of the example
68
Explanation
1 Byte ( 8 bits ) | ||||||||||
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | = | 77 |
$y | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | = | 198 |
In the above table, the value set for $x(77) in the 1st, 3rd, 4th and 7th place. The place values are 1, 4, 8 and 64 and the value set for $y(198) in the 2nd, 3rd, 7th and 8th place with the corresponding place value 2, 4, 64 and 128.
So you can see from the above table that, bits those are shared by $x and $y together is the 3rd and 7th bits. So 64 + 4 = 68 returns.
View the example of php bitwise AND with two shared bit in the browser
Bitwise OR
Example of PHP bitwise OR
<?php $x=5; $y=11; echo $x | $y; ?>
Output of the example
15
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | = | 5 |
$y | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | = | 11 |
In the above table, the value set for $x(5) in the 1st and 3rd place. The place value respectively are 1 and 4, and the value set for $y(11) in the 1st, 2nd and 4th place with the corresponding place value 1, 2 and 8.
So you can see from the above table that, the $x and $y sets together either 1st or 2nd or 3rd or 4th bits. So return value is the addition of place value of the sets bits, that is 8+4+2+1=15.
View the example of php bitwise OR in the browser
Bitwise XOR
The Xor operator also performs a bitwise comparison in two numeric expressions and sets the corresponding bit in the result. When one and only one of the expression evaluates to true the result is true.
The below table shows how a XOR operation performs.
Expression1 | Expression2 | Result |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | False |
The below table shows the bitwise comparison of bitwise XOR -
Bit in Expression1 | Bit in Expression2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example of PHP bitwise XOR
<?php $x=12; $y=11; echo $x ^ $y; ?>
Output of the example
7
>Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | = | 12 |
$y | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | = | 11 |
In the above table, the value set for $x(12) in the 3rd and 4th place. The place value respectively are 4 and 8, and the value sets for $y(11) in the 1st, 2nd and 4th place with the corresponding place value 1, 2 and 8.
So you can see from the above table that, the $x and $y sets together either 1st or 2nd or 3rd or 4th bits but they shared together only a 4th bit. So return value is the addition of place value of the set bits but not the bit shared together, that is 4+2+1=7.
View the example of php bitwise XOR in the browser
Bitwise NOT
The below table will display how NOT operator performs on $x and $y and returns true when a set bit of one expression is not set in another expression.
Example of PHP bitwise NOT using after AND
<?php $x=12; $y=10; echo $x & ~ $y; ?>
Output of the example
4
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | = | 12 |
$y | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | = | 10 |
In the above table, the value set for $x(12) in the 3rd and 4th place. The place value respectively are 4 and 8, and the value sets for $y(10) in the 2nd and 4th place with the corresponding place value 2 and 8.
So you can see from the above table that, the $x and $y sets together either 1st or 2nd or 3rd or 4th bits but they shared together only a 4th bit. So return value is the 4, because of only bit sets in $x but not in $y.
View the example of php bitwise NOT using after AND in the browser
Example of PHP bitwise NOT using before AND
<?php $x=12; $y=10; echo ~ $x & $y; ?>
Output of the example
2
Explanation
In this case, the return value is 2 because the bit set on $y but not on $x.
View the example of php bitwise NOT using before AND in the browser
Bit Shifting
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.
Example of PHP Bit Shifting ( left shift )
<?php $x=8; $y=3; echo $x << $y; ?>
Output of the example
64
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | = | 8 |
Output | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | = | 64 |
In the above example, the value of $x that is 8 is taken and a BIT SHIFT LEFT operation is performed. So, 8 is multiplied by 2 thrice. Thus we get 8 x 2 x 2 x 2 = 64.
View the example of php Bit Shifting ( left shift ) in the browser
Advance example of PHP Bit Shifting ( left shift )
<?php $x=12; $y=4; echo $x << $y; ?>
Output of the example
192
Explanation
In the above example, value of $x that is 12 is taken and a BIT SHIFT LEFT operation is performed. So, 12 is multiplied by 2 four times. Thus we get 12 x 2 x 2 x 2 x 2 = 192.
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | = | 12 |
Output | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | = | 192 |
View the advance example of php Bit Shifting ( left shift ) in the browser
Example of PHP Bit Shifting ( right shift )
<?php $x=8; $y=3; echo $x >> $y; ?>
Output of the example
1
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | = | 8 |
Output | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | = | 1 |
In the above example, value of $x that is 8 is taken and a BIT SHIFT RIGHT operation is performed. So, 8 is divided by 2 three times. Thus we get 8/2=4/2=2/2 = 1.
View the example of php Bit Shifting ( right shift ) in the browser
Advance example of PHP Bit Shifting ( right shift )
<?php $x=96; $y=5; echo $x >> $y; ?>
Output of the example
3
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | = | 96 |
Output | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | = | 3 |
In the above example, value of $x that is 96 is taken and a BIT SHIFT RIGHT operation is performed. So, 96 is divided by 2 five times. Thus we get 96/2=48/2=24/2=12/2=6/2= 3.
View the advance example of php Bit Shifting ( right shift ) in the browser
Example of PHP Bit Shifting ( right shift ) exceeds step value
<?php $x=64; $y=7; echo $x >> $y; ?>
Output of the example
0
Explanation
1 Byte ( 8 bits ) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$x | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | = | 64 |
Output | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | = | 0 |
In the above example, value of $x that is 64 is taken and a BIT SHIFT RIGHT operation is performed. So, 64 is divided by 2 seven times. While dividing at a certain point we don't have anything left to divide. Thus the return is 0.
View the advance example of php Bit Shifting ( right shift ) exceed step value in the browser
Join our Question Answer community to learn and share your programming knowledge.
Help the community:
C++: Decimal to binary conversion
JavaScript: Need Help in JavaScript
Python: Help me with this program