In the last post, we learnt about Bits, Bytes and Binary Values. Before exploring the bitwise operators, I just wanted to clear the concepts about bits, bytes and binary values, for the people who don't know or know very less.
Bitwise operations are performed on the binary values and can be used on values of type long, int, short, char or byte. Bitwise operations can't be performed on boolean, float, double or class types. And the result of these operations are also only binary values. Then you can convert into decimal values.
Bitwise operations are performed on the binary values and can be used on values of type long, int, short, char or byte. Bitwise operations can't be performed on boolean, float, double or class types. And the result of these operations are also only binary values. Then you can convert into decimal values.
Bitwise AND (&) operator
Bitwise AND (&) operator is performed on two binary values and evaluate values bit by bit. If both bits are 1, then only it returns 1, else it returns 0. Here is the chart of Bitwise AND (&) operator -
First Bit
|
Second Bit
|
Result Bit
|
---|---|---|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
1
|
Suppose a = 60 (0011 1100) and b = 86 (0101 0110)
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | b = 86 |
0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | a&b = 20 |
Bitwise OR (|) operator
Bitwise OR (|) operator is performed on two binary values and evaluate values bit by bit. If either of bits is 1, then it returns 1, else it returns 0. Here is the chart of Bitwise OR (|) operator -
First Bit
|
Second Bit
|
Result Bit
|
---|---|---|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
1
|
Suppose a = 60 (0011 1100) and b = 86 (0101 0110)
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | b = 86 |
0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | a|b = 126 |
Bitwise XOR (^) operator
Bitwise XOR (^) operator is performed on two binary values and if both bits are same (either 0 or 1), then it returns 0, else it returns 1. It is also called exclusive OR operator. Here is the chart of Bitwise XOR (^) operator -
First Bit
|
Second Bit
|
Result Bit
|
---|---|---|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
0
|
Suppose a = 60 (0011 1100) and b = 86 (0101 0110)
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | b = 86 |
0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | a^b = 10 |
Unary NOT (~) operator
Bitwise Unary NOT (~) operator is performed on one binary value bit by bit and returns its opposite bit. Here is the chart of Unary NOT (~) operator -
Bit
|
Result Bit
|
---|---|
0
|
1
|
1
|
0
|
Suppose a = 60 (0011 1100)
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | ~a = 195 |
Shift Right (>>) operator
Shift Right (>>) operator shifts the bits to right side by specified number of bits, but doesn't change the leftmost bit. Leftmost bit or high-order bit is used to represent sign (+ / -) of the value.* So, the sign won't get changed.
Suppose a = 60 (0011 1100) and shift by 1 bit
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | a>>1 = 30 |
Suppose a = -53 (1011 1100) and shift by 1 bit
1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = -53 |
1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | a>>1 = -27 |
Unsigned Shift Right (>>>) operator
Unsigned Shift Right (>>>) operator shifts the bits to right side by specified number of bits without preserving the sign of the integer number and the leftmost bits are replaced by 0s. This operator is also called Zero-fill Shift Right operator.Suppose a = 181 (1011 1100) and shift by 1 bit
1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 188 |
0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | a>>>1 = 94 |
Shift Left (<<) operator
Shift Left (<<) operator shifts the bits to left side by specified number of bits and the rightmost bits are replaced by 0s. Here is the example of Shift Left (<<) operator -Suppose a = 60 (0011 1100) and shift by 1 bit
0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | a = 60 |
0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | a<<1 = 120 |
Bitwise Assignment Operators
Operator | Code | Operation |
---|---|---|
&= | x &= y | x = x & y |
|= | x |= y | x = x | y |
^= | x ^= y | x = x ^ y |
>>= | x >>= y | x = x >> y |
>>>= | x >>>= y | x = x >>> y |
<<= | x <<= y | x = x << y |
As you know, the binary numbers have lowest-order bit as 1 when number is odd.
Class EvenOdd (EvenOdd.java)
import java.util.Scanner; public class EvenOdd { public static void main(String args[]) { String strEvenOdd; int number; System.out.println("Please enter the number:"); Scanner sc = new Scanner(System.in); number = sc.nextInt(); if((number & 1) == 0) strEvenOdd = "Even"; else strEvenOdd = "Odd"; System.out.println("Number " + number + " is " + strEvenOdd + "."); } }If the lowest-order bit is 1, the number is odd and if it is 0, the number is even. We are checking returned value after AND operation with 1, if it returns 1 then number is odd, else the number is even.
Suppose user enters the number 5 = 0000 0101
0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | number = 5 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | number&1 = 1 |
Or user enters the number 8 = 0000 1000
0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | number = 8 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | number&1 = 0 |
*We have learnt about all the data types and their ranges in the post Data Types. Negative numbers are usually represented by setting the high-order bit of an integer value to 1.
NOTE: I am going to discuss more on signed and unsigned integers and how to do manipulate binary values in my coming posts. Here we used 8-bit (byte) in examples, so we will use byte data type.
Example 1: What should be the value of b in the following statement?
byte a = -1;Solution :
byte b = (byte)(-1 >> 1);
a = -1 (1111 1111) and shift by 1 bit (This is signed right shift, so highest order bit will be preserved as 1 for negative values)
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | a = -1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | a>>1 = -1 |
Example2 : How can you use bitwise operator for division or multiplication? Write a program.
Solution :
When you use the right shift operator, it returns half of the number for each bit.
1 bit shifted -> number * 1/2
2 bits shifted -> number * 1/2 * 1/2 -> number * 1/4
3 bits shifted -> number * 1/2 * 1/2 * 1/2 -> number * 1/8
and so on....
When you use the left shift operator, it returns double of the number for each bit.
1 bit shifted -> number * 2
2 bits shifted -> number * 2 * 2 -> number * 4
3 bits shifted -> number * 2 * 2 * 2 -> number * 8
and so on....
Class BitTest (BitTest.java)
public class BitTest { public static void main(String args[]) { int number = 100; int shiftBy = 2; int rightShift = number >> shiftBy; int leftShift = number << shiftBy; System.out.println("Number: " + number); System.out.println("Shifted By " + shiftBy + " bits"); System.out.println(); System.out.println("Right Shift: " + rightShift); System.out.println("Left Shift: " + leftShift); } }
NOTE: When number is shifted right, it is divided by 2 to the power of shiftBy (no. of bits shifted). And when number is shifted left, it is multiplied by 2 to the power of shiftBy (no of bits shifted)
No comments:
Post a Comment