Bitwise Operators

Java bitwise operators can be applied to the integer types: long, int, short, char, byte. Bitwise Operators act upon the individual bits of their operands.

OperatorResult
~Bitwise unary NOT
& Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|=Bitwise OR assignment
^=Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment

Left Shift

It has this general form:

  value << num  

The following code shifts byte type variable.

 

public class Main {
  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);
  }
}

  

The output generated by this program is shown here:


Original value of a: 64
i and b: 256 0

Each left shift has the effect of doubling the original value. The following program illustrates this point:

 

public class Main {
  public static void main(String args[]) {
    int num = 0xFFFFFFF;

    for (int i = 0; i < 4; i++) {
      num = num << 1;
      System.out.println(num);

    }
  }
}

 

The program generates the following output:


536870910
1073741820
2147483640
-16

The Right Shift

>, shifts all of the bits in a value to the right a specified number of times. Its general form is shown here: ]]>

  value >> num  

num specifies the number of positions to right-shift.

The following code fragment shifts the value 32 to the right by two positions:

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

    int a = 32;
    a = a >> 2;
    System.out.println("a is " + a);

  }
}

The output:


a is 8

The Unsigned Right Shift

>>, always shifts zeros into the high-order bit. ]]>

  

public class Main {
  public static void main(String[] argv) {
    int a = -1;
    a = a >>> 24;

    System.out.println("a is " + a);
  }
}

The output:


a is 255

Bitwise Operator Assignments

Bitwise operator assignments combines the assignment with the bitwise operation. The following two statements are equivalent:

 

a = a >> 4; 
a >>= 4;  

The following two statements are equivalent:


a = a | b; 
a |= b;

The following program demonstrates the bitwise operator assignments:

  
public class Main {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;
    a |= 2;
    b >>= 2;
    c <<= 2;
    a ^= c;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);

  }
}
  

The output of this program is shown here:


a = 15
b = 0
c = 12

Using the Bitwise Logical Operators

The following program demonstrates the bitwise logical operators:

  

public class Main {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    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 = " + a);
    System.out.println(" b = " + b);
    System.out.println(" a|b = " + c);
    System.out.println(" a&b = " + d);
    System.out.println(" a^b = " + e);
    System.out.println("~a&b|a&~b = " + f);
    System.out.println(" ~a = " + g);

  }
}  

Here is the output from this program:

  
 a = 1
 b = 2
 a|b = 3
 a&b = 0
 a^b = 3
~a&b|a&~b = 3
 ~a = 14
  
Home 
  Java Book 
    Language Basics  

Operators:
  1. Operators
  2. Arithmetic Operators
  3. Bitwise Operators
  4. Relational Operators
  5. Boolean Logical Operators
  6. The ? Operator
  7. Operator Precedence