Author: SAI K
Core Java
In this chapter, we'll learn Bitwise operators in Java programming language, their syntax, and how to use them with examples.
Java defines several bitwise operators that can be applied to the integer types: long
,
int
, short
, char
, and byte
. These operators act upon the
individual bits of their operands.
Let's summarize all the bitwise operators as:
The bitwise logical operators are &
, |
, ^
, and ~
. Let's
briefly discuss these bitwise logic operators. The following table shows the outcome of each operation.
Also called the bitwise complement, the unary NOT operator, ~
, inverts all of the bits of its
operand.
The AND operator, &
, produces a 1 bit if both operands are also 1. A zero is produced in all
other cases.
The OR operator, |
, combines bits such that if either of the bits in the operands is a 1, then
the resultant bit is a 1.
The XOR operator, ^
, combines bits such that if exactly one operand is 1, then the result is 1.
Otherwise, the result is zero.
The following program demonstrates the bitwise logical operators:
package net.javaguides.corejava.operators.bitwise;
public class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};
int a = 3; // 0011 in binary
int b = 6; // 0110 in binary
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 = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
The left shift operator, <<
, shifts all of the bits in a value to the left a specified
number of times.
package net.javaguides.corejava.operators.bitwise;
public class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte)(a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
Original value of a: 64
i and b: 256 0
The Java right shift operator >>
is used to move the left operand's value to the right by
the number of bits specified by the right operand.
class OperatorExample {
public static void main(String args[]) {
System.out.println(10 >> 2); // 10/2^2 = 10/4 = 2
System.out.println(20 >> 2); // 20/2^2 = 20/4 = 5
System.out.println(20 >> 3); // 20/2^3 = 20/8 = 2
}
}
2
5
2
All of the binary bitwise operators have a compound form similar to that of the algebraic operators, which combines the assignment with the bitwise operation.
package net.javaguides.corejava.operators.bitwise;
public class OpBitEquals {
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
a = 3
b = 1
c = 6