0% found this document useful (0 votes)
5 views16 pages

Bit Wise Operators-Java

The document explains the concepts of signed and unsigned binary numbers, detailing their representations in signed magnitude, 1's complement, and 2's complement forms. It also covers binary addition, bitwise operators in Java, and the use of complement operators for subtraction. Additionally, it describes left shift, signed right shift, and unsigned right shift operations with examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Bit Wise Operators-Java

The document explains the concepts of signed and unsigned binary numbers, detailing their representations in signed magnitude, 1's complement, and 2's complement forms. It also covers binary addition, bitwise operators in Java, and the use of complement operators for subtraction. Additionally, it describes left shift, signed right shift, and unsigned right shift operations with examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Signed and unsigned numbers

Binary Unsigned Signed 1’s complement 2’s complement


numbers magnitude
000 0 0 0 0
001 1 1 1 1
010 2 2 2 2
011 3 3 3 3
100 4 0 -3 -4
101 5 -1 -2 -3
110 6 -2 -1 -2
111 7 -3 0 -1

Unsigned numbers are the numbers greater than or equal to 0


Signed numbers are ranging from -ve to +ve numbers
For signed numbers, the most significant bit for negative numbers
in the binary form is 1 and for positive numbers in the binary form
is 0
Signed numbers can be represented in following
 signed magnitude form
 1’s complement form
 2’s complement form
In the above table, unsigned numbers are obtained by taking the
decimal form of the binary numbers mentioned in the 1st column
Signed magnitude form is obtained as below
The signed magnitude for binary numbers 000,001,010,011 are +ve
numbers as the most significant bit is 0
Most significant bit in the binary number is the left most binary bit
Example:
001
The most significant bit is 0 in the above binary number
How to convert signed magnitude form binary to decimal
Leave the most significant bit and take the other bits and convert to
decimal
If the most significant bit is 1, add – to the decimal number obtained
If the most significant bit is 0, add + to the decimal number obtained
Example:
How to convert 010 to decimal:
Leave 0 which is MSB and take the other bits and convert to decimal
The value obtained in converting 10 to decimal is 2. Since MSB is 0,
the value is +2
Example:
How to convert to decimal 110:
Leave 1 which is MSB and take the other bits and convert to decimal
The value obtained in converting 10 to decimal is 2. Since MSB is 1,
the value is -2
How to convert 1’s complement binary form to decimal
From the given binary number, take complement of each bit in
binary like 0 to 1 and 1 to 0, convert to decimal. If the MSB of the
original binary number (before taking complement) is 1, add – to the
decimal number else add + to the decimal number
Example:
Convert 101 to 1’s complement form
 Take each bit complement form of 101 which becomes 010
 Convert 010 to decimal which is 2. Since MSB of original binary
number (101, before taking complement) is 1, add – to decimal
which is -2
 Hence decimal value for 101 in 1’s complement form is -2
How to convert 2’s complement form in binary to decimal
Approach 1:
Take 1’s complement of binary form and add 1 to it, convert to
decimal. If the MSB of the original binary number is 1, add – to the
decimal number else add + to the decimal number
Example:
Convert 2’s complement of 100 to decimal
 Take 1’s complement of 100 which is 011
 Add 1 to 011 which is 100
 Now convert 100 to decimal which is 4. Since MSB of original
binary number is 1, add – to the decimal number which is -4
Approach 2:
Example:
Convert 2’s complement of 100 to decimal
 Since 1 is MSB and indicates -ve number
 We can convert to decimal like this as below
 (-1) *2^2 + 0 * 2^1 + 0 * 2^0 = -4
Note:
2’s complement is more efficient when compared to signed
magnitude and 1’s complement form as 2’s complement have unique
decimal numbers for each binary number
In 1’s complement, 0 is represent by 2 binary numbers (111 and
000).
In signed magnitude, 0 is represent by 2 binary numbers (100 and
000).

Adding two binary numbers


Resu
Input bit Input bit lt Carry
1 1 0 1
1 0 1 0
0 1 1 0
0 0 0 0

Bit wise operators in Java


Bitwise operators in Java work on the binary form of the integer
numbers (int, long, short, byte)
 Bitwise Complement operator
 Left shift operator
 Signed Right shift operator
 Unsigned Right shift operator
 Bitwise OR operator
 Bitwise AND operator
 Bitwise XOR operator

Bitwise Complement Operator


This is a unary operator that is there is only one operand
int x = 25
int y = ~x // bitwise complement of x which is 1’s complement
Bitwise complement operator makes 0 to 1 and 1 to 0 of the binary
number
Formula
If there is an integer n, the complement of the number is -(n+1)
Example 1:
 int x = 25
 int y = ~x
 y = -(25+1) = - 26
Example 2:
 int x = 31
 int y = ~x
 y = -(31+1) = -32
Example 3:
 int x = 65
 int y = ~x
 y = -(65+1) = -66
Example 4:
 int x = -11
 int y = ~x
 y = -(-11+1) = -(-10) = 10
Example 5:
 long x = -13
 long y = ~x
 y = -(-13+1) = -(-12) = 12

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

Use of Complement Operator


The use of complement operator is that subtraction of two integers
can be taken through addition operation only.
This will save the cost of subtraction circuit in the digital circuits in
the computer processor
X-Y = X+(2’s complement of Y)
Example:
 int x = 10
 int y = 3
 x-y = x+(2’s complement of y)
 convert x to binary
 convert y to binary and take 2’s complement
 Rules to be followed
 If carry = 1 then the result is +ve (positive) and ignore the
carry
 If carry=0 the result is -ve (negative) and take 2's
complement of result
 10-3 = 10+(2's complement of 3)
 10 =01010 base 2
 Find 2's complement of 3
 convert 3 to binary= 011
 2's complement of 3 = 1's complement of 3 + 1 = 100 + 1 = 101
 10 + 2's complement of 3 = 01010 + 101
01010
+ 101
111 --- Ignore the carry
 111 converted to decimal is 7 which is equal to 10-3
 In this way subtraction is done by addition of 2’s complement

Bit Shift Operators


Left shift operator:
The left shift operator shifts all bits towards the left by a certain
number of specified bits
Left shift operator is represent by <<
Example 1
int x = 6
int y = x << 2
what is y value?
 Convert 6 to binary which is 0110
 Add 2 0’s to right side of binary number because in the
question value 2 is specified (x << 2)
 This becomes 011000
 Convert 011000 to decimal
 011000 to decimal = 24
 Hence y = 24 --- answer

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

Bitwise AND operator


Bitwise AND operator (&) return 0 if at least one of the operands is 0
The truth table is given below
a b a&b

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

Bitwise XOR operator


The XOR operator (^) returns 1 if only one of the operands is 1
Truth table
a b a^b

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

You might also like