You are currently viewing Bit Operators In Java

Bit Operators In Java

1. Introduction

Bit operators in Java are like secret agents working behind the scenes, manipulating individual bits of data to perform powerful operations. While they may seem mysterious at first, understanding how bit operators work is essential for unlocking the full potential of Java programming.

2. Integer BitWise Operators

Bitwise operators apply to each operand bit individually. Among these operators, only one is unary ~ (bitwise complement) and the others are binary: & (bitwise AND), | (bitwise inclusive OR), ^ (bitwise exclusive OR). You should note that the operators &, |, and ^ are overloaded. They can be applied to boolean operands to perform boolean logical operations.
The table below shows the semantics of these operators:

OperatorNotationMeaning
Bitwise Complement~aInvert the bit value. 1 becomes 0, 0 becomes 1
Bitwise ANDa & b1 if both a and b are 1, 0 otherwise
Bitwise ORa | b1 if either a or both a and b are 1, 0 otherwise
Bitwise XORa ^ b1 if and only if one of the bits is 1, 0 otherwise

Let’s consider two binary values a and b. The table below shows the result of applying each of these operators to the possible values of a and b.

ab~aa & ba | ba ^ b
110110
100011
011011
001000

2.1. Bitwise Operations Example

        int a = 5;//0...101
        int b = 4;//0...100
        int complementA = ~a;//1...010 which is -6
        int bitwiseAND = a & b;//0...100 which 4
        int bitwiseOR = a | b;//0...101 which 5
        int bitwiseXOR = a ^ b;//0...001 which is 1;

2.2. Bitwise Compound Assignment Operators: &=, |=, ^=

These operators are a shortcut to enable bitwise operation and assignment in one go. They can be defined as follows:

  • a &= b is equivalent to a = a & b
  • a |= b is equivalent to a = a | b
  • a ^= b is equivalent to a = a ^ b

3. Shift Operators

These binary operators are used to perform bit shifts on the left operand. The right operand determines the number of bits to shift. Before proceeding with the shift, the bit distance is calculated by ANDing the right operand with the value 0x1f(31) if it is an integer operation. If the operation is carried out on long type data, the AND mask is 0x3f(63). The consequence of all this is that the shift distance is always between 0 and 31 for int types, and between 0 and 63 for long types.

OperatorNotationMeaning
Shift leftx << nShift all bits in x left n times, filling with 0 from the right
Shift right with sign bitx >> nShift all bits in x right n times, filling with the sign bit from the left
Shift right with zero fillx >>> nShift all bits in x right n times, filling with 0 from the left

3.1. Shift Operations Example

        int a = 4;//0...100
        int b = 8;//0...1000
        int c = -6; //1...010
        int leftShift = a << 1;//0...1000 which is 8
        int rightShiftWithSign = b >> 1;//0...100 which 4
        int rightShiftWithZero = c >>> 1;//1...101 which 2147483645

Observations

  • Each left shift of one bit corresponds to a multiplication by 2
  • Each right shift of one bit with the sign bit corresponds to a division by 2

3.2. Shift Compound Assignment Operator: <<=, >>=, >>>=

Just like Bitwise operators, these operators are a shortcut to enable a bit shift operation and assignment in one go.

  • a <<= n is equivalent to a = a << n
  • a >>= n is equivalent to a = a >> n
  • a >>>= n is equivalent to a = a >>> n

4. Operator Precedence and Associativity

Read about the Precedence and Associativity Rules for Bit Operators in this article.

5. Conclusion

Congratulations! You’ve now mastered the fundamentals of bit operators in Java. These tiny but mighty operators are the key to performing intricate bitwise operations and unleashing the full potential of your Java programs.

6. References

1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has One Comment

Leave a Reply