Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Bitwise Operators

Bitwise operators in Java perform operations on individual bits of integer data types (byte, short, int, and long). These operators are useful when you need to manipulate binary data or perform low-level operations.

In this tutorial, we will go through the bitwise operators available in Java:

  • Bitwise AND (&):

The bitwise AND operator performs a logical AND operation on corresponding bits of two integer values. If both bits are 1, the resulting bit will be 1, otherwise, it will be 0.

int a = 5;     // binary: 0101
int b = 3;     // binary: 0011
int result = a & b; // binary: 0001 (decimal: 1)
  • Bitwise OR (|):

The bitwise OR operator performs a logical OR operation on corresponding bits of two integer values. If at least one of the bits is 1, the resulting bit will be 1, otherwise, it will be 0.

int a = 5;     // binary: 0101
int b = 3;     // binary: 0011
int result = a | b; // binary: 0111 (decimal: 7)
  • Bitwise XOR (^):

The bitwise XOR operator performs a logical XOR operation on corresponding bits of two integer values. If the bits are different, the resulting bit will be 1, otherwise, it will be 0.

int a = 5;     // binary: 0101
int b = 3;     // binary: 0011
int result = a ^ b; // binary: 0110 (decimal: 6)
  • Bitwise NOT (~):

The bitwise NOT operator performs a logical NOT operation on each bit of an integer value. If the bit is 1, it will be changed to 0, and vice versa.

int a = 5;          // binary: 0000...0101
int result = ~a;    // binary: 1111...1010 (decimal: -6)

Note that the result of the bitwise NOT operation is in two's complement form, which is why the result is -6 in this example.

  • Left Shift (<<):

The left shift operator shifts the bits of an integer value to the left by a specified number of positions. The empty positions on the right are filled with zeros.

int a = 5;     // binary: 0000...0101
int result = a << 2; // binary: 0000...10100 (decimal: 20)
  • Right Shift (>>):

The right shift operator shifts the bits of an integer value to the right by a specified number of positions. The empty positions on the left are filled with the sign bit (0 for positive numbers and 1 for negative numbers).

int a = 20;    // binary: 0000...10100
int result = a >> 2; // binary: 0000...0101 (decimal: 5)
  • Unsigned Right Shift (>>>):

The unsigned right shift operator shifts the bits of an integer value to the right by a specified number of positions. The empty positions on the left are filled with zeros, regardless of the sign of the original number.

int a = -15;   // binary: 1111...11110001
int result = a >>> 2; // binary: 0011...11111100 (decimal: 1073741820)
  1. Bitwise AND Operator in Java: The bitwise AND operator (&) performs a bitwise AND operation between corresponding bits.

    int result = 5 & 3; // Binary: 101 & 011 = 001
    
  2. Bitwise OR Operator in Java: The bitwise OR operator (|) performs a bitwise OR operation between corresponding bits.

    int result = 5 | 3; // Binary: 101 | 011 = 111
    
  3. Bitwise XOR Operator in Java: The bitwise XOR operator (^) performs a bitwise XOR operation between corresponding bits.

    int result = 5 ^ 3; // Binary: 101 ^ 011 = 110
    
  4. Java Bitwise NOT Operator: The bitwise NOT operator (~) inverts the bits.

    int result = ~5; // Binary: ~101 = 11111111111111111111111111111010
    
  5. Shifting Bits in Java with the Left Shift Operator: The left shift operator (<<) shifts bits to the left.

    int result = 5 << 2; // Binary: 101 << 2 = 10100
    
  6. Right Shift Operator in Java: The right shift operator (>>) shifts bits to the right, preserving the sign bit.

    int result = -8 >> 2; // Binary: -1000 >> 2 = -10
    
  7. Unsigned Right Shift Operator in Java: The unsigned right shift operator (>>>) shifts bits to the right, filling with zero.

    int result = -8 >>> 2; // Binary: -1000 >>> 2 = 1073741821
    
  8. Using Bitwise Operators for Flag Manipulation in Java: Bitwise operators are commonly used for setting, clearing, and checking flags.

    int flags = 0b00000110; // Flags: A=0, B=1, C=1
    flags |= (1 << 2); // Set flag B
    
  9. Bitwise Operations and Masking in Java: Bitwise AND with a mask is used for isolating specific bits.

    int data = 0b11011010;
    int mask = 0b00001111;
    int result = data & mask; // Masking to get the lower nibble
    
  10. Combining Bitwise Operations in Java: Bitwise operations can be combined for complex manipulations.

    int value = 0b11010110;
    value = (value & 0b11110000) | 0b00001111; // Clear upper nibble, set lower nibble
    
  11. Bitwise Operators and Conditional Statements in Java: Bitwise operators can be used in conditional statements.

    int value = 0b11010110;
    if ((value & 0b00001000) != 0) {
        // Check if a specific bit is set
    }
    
  12. Converting Decimal to Binary Using Bitwise Operators in Java: Bitwise operators can be used to convert decimal to binary.

    int decimal = 10;
    String binary = Integer.toBinaryString(decimal);
    
  13. Java Bitwise Operators and Boolean Values: Bitwise operators can be applied to boolean values for logical operations.

    boolean a = true;
    boolean b = false;
    boolean result = a & b; // Bitwise AND for boolean values
    
  14. Checking If a Specific Bit Is Set in Java: Use bitwise AND to check if a specific bit is set.

    int value = 0b11010110;
    if ((value & (1 << 3)) != 0) {
        // Check if the 4th bit is set
    }
    
  15. Bitwise Operations with byte, short, and int in Java: Bitwise operations can be performed on byte, short, and int data types.

    byte byteValue = 0b1101;
    byte result = (byte) (byteValue & 0b0011); // Bitwise AND for bytes
    
  16. Using Bitwise Operators for Low-Level Optimizations in Java: Bitwise operators are sometimes used for low-level optimizations.

    int x = 5;
    int y = 8;
    int sum = (x & y) + ((x ^ y) << 1); // Efficient addition without using +
    
  17. Handling Negative Numbers with Bitwise Operators in Java: Bitwise operators handle negative numbers in their binary representation.

    int negativeValue = -5;
    int result = negativeValue >> 1; // Right shift for division by 2
    
  18. Common Mistakes with Java Bitwise Operators: Be cautious about common mistakes such as misusing the bitwise NOT operator or confusing it with the logical NOT.

    int x = 5;
    int result = ~x; // Potential mistake, check for unintended bitwise NOT