Java Bitwise Operators
In this chapter you will learn:
- What are bitwise operation
- How to shift bits in a value to the left
- How to shift bits in a value to the right
- How to use unsigned right shift
- How to use bitwise operator assignments
- How to use the Bitwise Logical Operators
Bitwise operation
Bitwise Operators act upon the individual bits of their operands.
Java bitwise operators can be applied to the integer types: long, int, short, char, byte
.
The following table lists all Java bitwise operators.
Operator | Result |
---|---|
~ | Bitwise unary NOT |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
>> | Shift right |
>>> | Shift right zero fill |
<< | Shift left |
&= | Bitwise AND assignment |
|= | Bitwise OR assignment |
^= | Bitwise exclusive OR assignment |
>>= | Shift right assignment |
>>>= | Shift right zero fill assignment |
<<= | Shift left assignment |
Left Shift Operator
The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times.
It has this general form:
value << num
The following code shifts byte type variable.
public class Main {
public static void main(String args[]) {
byte a = 64, b;
int i;/* ja v a 2s .c o m*/
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
The output generated by this program is shown here:
Each left shift has the effect of doubling the original value. The following program illustrates this point:
public class Main {
public static void main(String args[]) {
int num = 0xFFFFFFF;
/*from j a v a 2s . c o m*/
for (int i = 0; i < 4; i++) {
num = num << 1;
System.out.println(num);
}
}
}
The program generates the following output:
Right Shift Operator
The right shift operator, >>
, shifts all of the bits in a value to the right a
specified number of times. Its general form is shown here:
value >> num
num
specifies the number of positions to right-shift.
The following code fragment shifts the value 32
to the right by two positions:
public class Main {
public static void main(String[] argv) {
//from j ava 2s . c om
int a = 32;
a = a >> 2;
System.out.println("a is " + a);
}
}
The output:
Unsigned Right Shift
Java's unsigned, shift-right operator, >>>
, always shifts zeros into the
high-order bit.
public class Main {
public static void main(String[] argv) {
int a = -1;/* j ava 2 s . c o m*/
a = a >>> 24;
System.out.println("a is " + a);
}
}
The output:
Bitwise Operator Assignments
Bitwise operator assignments combines the assignment with the bitwise operation. The following two statements are equivalent:
a = a >> 4;
a >>= 4;
The following two statements are equivalent:
a = a | b;
a |= b;
The following program demonstrates the bitwise operator assignments:
public class Main {
public static void main(String args[]) {
int a = 1;/*from j a v a 2 s . c om*/
int b = 2;
int c = 3;
a |= 2;
b >>= 2;
c <<= 2;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
The output of this program is shown here:
Bitwise Logical Operators
The following program demonstrates the bitwise logical operators:
public class Main {
public static void main(String args[]) {
int a = 1;/* j a v a 2 s . c o m*/
int b = 2;
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("~a&b|a&~b = " + f);
System.out.println(" ~a = " + g);
}
}
Here is the output from this program:
Next chapter...
What you will learn in the next chapter: