Java Bitwise Operators

Introduction

Java has bitwise operators for the integer types: long, int, short, char, and byte.

The bitwise operators work on the individual bits of the operands.

Java bitwise operators are summarized in the following table:

Operator Result
~ 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
public class Main {
    public static void main(String[] args) {
        byte x, y;
        x = 10;//from www  .j  av a  2 s  .c o m
        y = 11;
        System.out.println("X = " + x + " Y = " + y);
        System.out.println("~X    = " + (~x));
        System.out.println("x&y   = " + (x & y));
        System.out.println("x|y   = " + (x | y));
        System.out.println("x^y   = " + (x ^ y));
        System.out.println("X<<2  = " + (x << 2));
        System.out.println("X>>2  = " + (x >> 2));
        System.out.println("X>>>2 = " + (x >>> 2));
    }
}



PreviousNext

Related