Java Bitwise Operators

Java bitwise operators manipulate the bits of binary numbers. Java supports the following six bitwise operators:

The "left shift" and "right shift" have already been discussed in the previous two posts.

This table shows the outcome of each operation. In the discussion that follows, always keep in mind that the bitwise operators are applied to each individual bit within each operand.

A B A | B A & B A ^ B ~A
0 0 0 0 0 1
1 0 1 0 1 0
0 1 1 0 1 1
1 1 1 1 0 0

The bitwise AND operator in Java

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

Following is an example that demonstrates the bitwise AND operator:

  0010101042
& 0000111115
 ___________
  0000101010

The bitwise OR operator in Java

The bitwise OR operator, |, combines bits so that if either of the bits in the operands is 1, then the result bit is also 1.

Following is an example that demonstrates the bitwise OR operator:

  0010101042
| 0000111115
 ___________
  0010111147

The bitwise NOT operator in Java

The bitwise NOT operator, also called the bitwise complement, the unary NOT operator (˜), inverts all of the bits of its operand.

For example, the number 42 has this bit pattern:

00101010

becomes

11010101

after the NOT operator is applied.

The bitwise XOR operator in Java

The bitwise XOR operator, ^, combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero.

The following example shows the effect of the bitwise XOR (^) operator. This example also demonstrates a useful attribute of a XOR operation. Notice here how the bit pattern of 42 is inverted wherever the second operand has a 1 bit. Wherever the second operand has 0 bit, the first operand is unchanged. You will find this property useful when performing some types of bit manipulations.

  0010101042
^ 0000111115
_________ 
  0010010137

The Bitwise Logical Operators Example

The following program demonstrates the bitwise logical operators:

Java Code
public class BitwiseOperatorsExample {
    public static void main(String[] args) {
        int a = 0b10101010;
        int b = 0b01010101;
        
        int resultAnd = a & b;
        System.out.println("a & b = " + Integer.toBinaryString(resultAnd));
        
        int resultOr = a | b;
        System.out.println("a | b = " + Integer.toBinaryString(resultOr));
        
        int resultXor = a ^ b;
        System.out.println("a ^ b = " + Integer.toBinaryString(resultXor));
        
        int resultNotA = ˜a;
        System.out.println("˜a = " + Integer.toBinaryString(resultNotA));
        
        int resultLeftShift = a << 2;
        System.out.println("a << 2 = " + Integer.toBinaryString(resultLeftShift));
        
        int resultRightShift = a >> 2;
        System.out.println("a >> 2 = " + Integer.toBinaryString(resultRightShift));
    }
}
Output
a & b = 0
a | b = 11111111
a ^ b = 11111111
˜a = 1010101
a << 2 = 1010101000
a >> 2 = 101010

This (aforementioned) Java code shows how to use bitwise operators to perform various operations on two integer variables, a and b, which represent their binary values of 0b101010 (170 in decimal) and 0b01010101 (85 in decimal), respectively.

The following is another example program in Java that demonstrates the bitwise operators.

Java Code
public class JavaProgram {
    private static final String[] BINARY_VALUES = {
        "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
        "1000", "1001", "1010", "1011", "1100", "1110", "1111"
    };

    public static void main(String[] args) {
        int binaryA = 0b0011;
        int binaryB = 0b0110;
        int bitwiseOr = binaryA | binaryB;
        int bitwiseAnd = binaryA & binaryB;
        int bitwiseXor = binaryA ^ binaryB;
        int complexExpression = (˜binaryA & binaryB) | (binaryA & ˜binaryB);
        int bitwiseNot = ˜binaryA & 0x0f;

        System.out.println("binaryA = " + BINARY_VALUES[binaryA]);
        System.out.println("binaryB = " + BINARY_VALUES[binaryB]);
        System.out.println("binaryA | binaryB = " + BINARY_VALUES[bitwiseOr]);
        System.out.println("binaryA & binaryB = " + BINARY_VALUES[bitwiseAnd]);
        System.out.println("binaryA ^ binaryB = " + BINARY_VALUES[bitwiseXor]);
        System.out.println("(˜binaryA & binaryB) | (binaryA & ˜binaryB) = " + BINARY_VALUES[complexExpression]);
        System.out.println("˜binaryA = " + BINARY_VALUES[bitwiseNot]);
    }
}
Output
binaryA = 0011
binaryB = 0110
binaryA | binaryB = 0111
binaryA & binaryB = 0010
binaryA ^ binaryB = 0101
(˜binaryA & binaryB) | (binaryA & ˜binaryB) = 0101
˜binaryA = 1100

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this article? Share it!



Like/follow us on social media for updates!