Bitwise Operators in Java with Examples


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:

1. The Bitwise Logical Operators

The bitwise logical operators are &, |, ^, and ~. Let's briefly discuss these bitwise logic operators. The following table shows the outcome of each operation.

The Bitwise NOT

Also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of its operand.

The Bitwise AND

The AND operator, &, produces a 1 bit if both operands are also 1. A zero is produced in all other cases.

The Bitwise OR

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 Bitwise XOR

The XOR operator, ^, combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero.
Let's demonstrates above bitwise logical operators with an example.

2. Using the Bitwise Logical Operators

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; // 0 + 2 + 1 or 0011 in binary
        int b = 6; // 4 + 2 + 0 or 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]);
    }

}
Output:
 a = 0011
 b = 0110
 a|b = 0111
 a&b = 0010
 a^b = 0101
~a&b|a&~b = 0101
 ~a = 1100

3. The Left Shift

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
Here, num specifies the number of positions to left-shift the value in value. That is, the << moves all of the bits in the specified value to the left by the number of bit positions specified by num.
The following program demonstrates this concept:
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);
    }
}
Output:
Original value of a: 64
i and b: 256 0

4. Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand. Its general form is shown here:
value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num.
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  
    }
}
Output:
2
5
2

5. Bitwise Operator Compound Assignments

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. 
For example, the following two statements, which shift the value in a right by four bits, are equivalent:
a = a >> 4;
a >>= 4;
Likewise, the following two statements, which result in a being assigned the bitwise expression a OR b, are equivalent:
a = a | b;
a |= b;
The following program creates a few integer variables and then uses compound bitwise operator assignments to manipulate the variables:
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);
    }
}
Output:
a = 3
b = 1
c = 6

Comments