ADVERTISEMENT
ADVERTISEMENT

Java Bitwise Operators

Learn about the Java bitwise operators, their usages, and examples.
Submitted by Nidhi, on March 08, 2022

Bitwise operators are used to manipulating bits of a number. We can use bitwise operators with any integral datatype like byte, short, int, long.

Types of bitwise operators

There are the following types of bitwise operators are used in Java,

  1. Bitwise AND (&)
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise Complement (~)
  5. Bitwise Left Shift (<<)
  6. Bitwise Right Shift (>>)
  7. Bitwise Unsigned Right Shift Operator (>>>)
OperatorSymbolExample
Bitwise AND&Operand1 & Operand2
Bitwise OR|Operand1 | Operand2
Bitwise XOR^Operand1 ^ Operand2
Bitwise Complement~~Operand1
Bitwise Left Shift<<Operand1 << Operand2
Bitwise Right Shift>>Operand1 >> Operand2
Bitwise Unsigned Right Shift Operator>>>Operand1 >>> Operand2

1) Bitwise AND Operator (&)

This is a binary operator, which is denoted by symbol '&'. It performs bit by bit AND operation on given number, if both bits are 1, then output will be 1, otherwise it will 0.

Operand1Operand2Operand1 & Operand2
111
1 0 0
0 1 0
0 0 0

Example:

public class Main {
  public static void main(String[] args) {

    int num1 = 5; // 0101
    int num2 = 7; // 0111

    int res = 0;

    res = num1 & num2;

    /*
        0101
        0111
        ====
        0101  That is 5
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: 5

2) Bitwise OR Operator (|)

This is a binary operator, which is denoted by symbol '|'. It performs bit by bit OR operation on given number, if any bit is 1, then output will be 1, otherwise it will 0.

Operand1 Operand2 Operand1 | Operand2
1 1 1
1 0 1
0 1 1
0 0 0

Example:

public class Main {
  public static void main(String[] args) {

    int num1 = 5; // 0101
    int num2 = 7; // 0111

    int res = 0;

    res = num1 | num2;

    /*
        0101
        0111
        ====
        0111  That is 7
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: 7

3) Bitwise XOR Operator (^)

This is a binary operator, which is denoted by symbol '^'. It performs bit by bit XOR operation on given number, if both bits are different, then output will be 1, otherwise it will 0.

Operand1 Operand2 Operand1 | Operand2
1 1 0
1 0 1
0 1 0
0 0 0

Example:

public class Main {
  public static void main(String[] args) {

    int num1 = 5; // 0101
    int num2 = 7; // 0111

    int res = 0;

    res = num1 ^ num2;

    /*
        0101
        0111
        ====
        0010  That is 2
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: 2

4) Bitwise Complement Operator (~)

This is a unary operator, which is denoted by symbol '~'. It performs bit by bit 1's complement operation on given number. It inverted the bits.

Operand ~Operand
1 0
0 1

5) Bitwise Left Shift Operator (<<)

It is a binary operator, which shifts the number bits to the left in given number. The left-shift operator multiplies the number of specified bits of power of 2 with the given number.

Example:

public class Main {
  public static void main(String[] args) {

    byte num = -5;
    byte res = 0;

    res = (byte)(num << 3);

    /*
        res = -5 * (2*2*2)
        res = -40
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: -40

6) Bitwise Right Shift Operator (>>)

It is a binary operator, which shifts the number bits to the right in given number. The right-shift operator divides the number of specified bits of power of 2 with the given number.

Example:

public class Main {
  public static void main(String[] args) {

    byte num = -64;
    byte res = 0;

    res = (byte)(num >> 3);

    /*
        res = -64 / (2*2*2)
        res = -8
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: -8

7) Bitwise Unsigned Right Shift Operator (>>>)

It is a binary operator, which shifts the number bits to the right in given number. The unsigned right-shift operator divides the unsigned number of specified bits of power of 2 with the given number.

Example:

public class Main {
  public static void main(String[] args) {

    byte num = 64;
    byte res = 0;

    res = (byte)(num >>> 3);

    /*
        res = 64 / (2*2*2)
        res = 8
    */

    System.out.println("Result: " + res);
  }
}

Output:

Result: 8
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.