Bit Wise Operators-Java
Bit Wise Operators-Java
Mathematical Proof:
int x = 25
int y = ~x
Convert x which is 25 to binary, that is 25 base 10 = 011001
base 2
The MSB should be 0 as 25 is +ve number
The variable y takes 1’s complement of x which is 1’s
complement of 011001 that becomes 100110
The variable y is 100110 which is 1’s complement of x
1’s complement of x is always 2’s complement of (x+1)
1’s complement of 25 in binary = 100110
Now take 2’s complement of 26 where x=25 and x+1=26
To perform 2’s complement of 26:
Convert 26 to binary
26 base 10 = 011010 base 2
MSB is 0 since 26 is +ve
Convert 011010 to 1’s complement
011010 to 1’s complement = 100101
2’s complement of 26 = 1’s complement of 011010 base 2 +
1 = 100101 + 1 = 100110
2’s complement of 26 in binary = 100110
1’s complement of 25 in binary = 2’s complement of 26 in
binary = -26 in decimal
Therefore, int x = 25, int y = ~x
Hence y=-26
Note: 2’s complement of x in binary = -x in decimal where x is integer
number
Example:
2’s complement of 56 in binary = -56 in decimal
2’s complement of 64 in binary = -64 in decimal
Example 2
int x = 8
int y = x << 1
convert 8 to binary which is 01000
Add one 0 to right side of binary number 01000 because x << 1
This becomes 010000
Convert 010000 to decimal which is 16
Hence y is 16
Example 3
int x = -8
int y = x << 1
convert -8 to binary
For negative number, we need to take 2’s complement form
2’s complement form of 8 in binary = binary form of -8
2’s complement of 8 in binary = 1’s complement of 8 in binary +
1
Binary number for 8 = 01000
1’s complement of 8 = 10111
Add 1 which 10111+1 = 11000
2’s complement of 8 = binary form of -8 = 11000
Now we need to compute -8 << 1
Take binary form of -8(11000 obtained above step) and add
one 0 to right side which is 110000
Now 110000 needs to be converted to decimal
Since the MSB is 1(2’s complement form) we can convert to
decimal in any of the 2 approaches mentioned in the above
sections, we will follow the 2nd approach
Since MSB is 1 that is -ve, (-1) * 2^5+ 1*2^4 +0+0+0+0 = -16
The answer is -16
Signed Right shift operator:
The signed right shift operator shifts all bits towards the right by a
certain number of specified bits. This will consider sign bit
Signed Right shift operator is represented by >>
Example 1
int x = 6
int y = x >> 2
what is y value?
Convert 6 to binary which is 0110
Remove 2 bits from right side because in the question, value 2
is specified (x >> 2)
This becomes 01
Add two 0’s to left side as the x value is +ve
This becomes 0001
Convert 0001 to decimal = 1
Hence y = 1 is the answer
Example 2
int x = -86
int y = x >> 2
what is y value?
Convert -86 to binary
For negative number, we need to take 2’s complement form
2’s complement form of 86 in binary = binary form of -86
2’s complement of 86 in binary = 1’s complement of 86 in
binary + 1
Binary number of 86 = 01010110
1’s complement of 86 = 10101001
Add 1 which 10101001+1 = 10101010
2’s complement of 86 = binary form of -86 = 10101010
Now we need to compute -86 >> 2
Take binary form of -86(10101010 obtained above step) and
remove 2 bits from right side which becomes 101010
Now add two 1’s to the left side of 101010 as x value is -ve
number
This becomes 11101010
Since this is in 2’s complement form we can convert to decimal
(-1) *2^7 + 1*2^6 + 1* 2^5 + 0 * 2^4 + 1* 2^3+ 0*2^2 + 1* 2^1
+ 0*2^0 = -128+64+32+8+2= -22
The answer is -22
Unsigned Right shift operator:
The unsigned right shift operator shifts all the bits towards the right
by a certain number of specified bits. This will not consider sign bit
and fill up zero’s on left side of binary number
Unsigned Right shift operator is represented by >>>
Unsigned Right shift operator works on 32 bits for int and 64 bits for
long
Example 1
int x = 52
int y = x >>> 2
what is y value?
Convert 52 to binary and make this to 32-bit form
This becomes 0000 0000 0000 0000 0000 0000 0011 0100
Remove 2 bits from right side because x>>>2 is given in the
question
This becomes 0000 0000 0000 0000 0000 0000 0011 01
Add two 0’s to left side
This becomes 00 0000 0000 0000 0000 0000 0000 0011 01
Convert this to decimal
The value is 13
Hence y answer is 13
Example 2
int x = -52
int y = x >>> 2
what is y value?
Convert -52 to binary in 32-bit form since x is integer
-52 in binary should be in 2’s complement form of 52 in binary
Convert 52 in binary which becomes 0000 0000 0000 0000
0000 0000 0011 0100(in 32-bit form)
Take 1’s complement
This becomes 1111 1111 1111 1111 1111 1111 1100 1011
Add 1 to this
This becomes 1111 1111 1111 1111 1111 1111 1100 1100
This is 2’s complement form of 52 in binary which is binary
form of -52 in binary in 32-bit form
Now -52>>> 2 to be performed
Remove 2 bits from right side and add two 0’s to left side of the
above 2’s complement binary form
This becomes 00 1111 1111 1111 1111 1111 1111 1100 11
This has to be converted to decimal
This becomes 1073741811 which is the answer
Example 3
long x = -52
long y = x >>> 2
what is y value?
Convert -52 to binary in 64 bit form since x is long type (8
bytes)
-52 in binary should be in 2’s complement form of 52 in binary
Convert 52 in binary which becomes 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011
0100(in 64-bit form)
Take 1’s complement
This becomes 1111 1111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1100 1011
Add 1 to this
This becomes 1111 1111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1100 1100
This is 2’s complement form of 52 in binary which is binary
form of -52 in 64-bit form
Now -52>>> 2 to be performed
Remove 2 bits from right side and add two 0’s to left side of the
above 2’s complement binary form
This becomes 00 1111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1100 11
This has to be converted to decimal
This becomes 4611686018427387891 which is the answer
Summary:
For Left shift operators, add the number of 0’s equal to the value
specified in the shift operation to the right side of binary number
Example: 13 << 2
Add 2 0’s to binary form of 13 to right side and convert to decimal,
this will be the answer
For signed right shift operators, from the binary number remove the
number of bits equal to the value specified in the signed right shift
operator from the right side and add equal number of 0’s to left side
if the number is +ve or add equal number of 1’s to left side if the
number is -ve
Example: 13 >> 2
Remove 2 bits from right side of 13 in binary form and add two 0’s to
left side as 13 is +ve
-13 >> 2
Remove 2 bits from right side of -13 binary number (2’s complement
of 13 in binary) and add two 1’s to left side as -13 is -ve
For unsigned right shift operator, take 32-bit form of binary number
if result is int or 64-bit form of binary number if result is long,
remove the number of bits equal to the value specified in the
unsigned right shift operator from right side and add equal number
of 0’s to left side if number is +ve or -ve and convert to decimal
Bitwise OR operator
Bitwise OR operator(|) return 1 if at least one of the operands is 1
Truth table
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1
Example 1
int x = 13
int y = 21
int z = x | y;
Bitwise OR operation on x and y
13 = 001101 (binary)
21 = 010101 (binary)
Result of bitwise OR as per above truth tables = 011101 =
convert this to decimal = 29
Example 2
int x = -13
int y = 21
int z = x | y;
Bitwise OR operation on x and y
x= -13 in binary form = 1’s complement of (13 in binary) + 1
-13 in binary form = 1’s complement (13 in binary) +1 = 1’s
complement (001101) + 1 = 110010+1 = 110011
y = 21 = 010101 (binary)
Perform bit wise “|” between each bit of x and y in binary as
per above truth table
Result of Bitwise OR operation as per above truth table =
110111 = 2’s complement form to decimal = (-1) *2^5 + 1*
2^4+0*2^3+1*2^2+1*2^1+1*2^0=
-32+16+4+2+1= -9
Answer is -9
0 0 0
0 1 0
1 0 0
1 1 1
Example 1
int x = 13
int y = 21
int z = x & y
13 in binary is 001101
21 in binary is 010101
Z = x & y = 001101 & 010101
Perform bit wise “&” between each bit of x and y in binary as
per above truth table
The result is 000101 = 5 in decimal
0 0 0
0 1 1
1 0 1
1 1 0
Example 1
int x = 13
int y = 21
int z = x ^ y
13 in binary is 001101
21 in binary is 010101
z = x ^ y = 001101 ^ 010101
Perform bit wise XOR between each bit of x and y in binary as
per above truth table
The result is 011000 = 24 in decimal