Java Bitwise Operators

We’ll cover the following

  • Bitwise Operators
  • Bitwise Logical Operators
  • Bitwise NOT
  • Bitwise AND
  • The Bitwise OR
  • Bitwise XOR
  • Example

Bitwise Operators

Java defines several bitwise operators which can be applied to the integer types, long,
int, short, char, and byte. These operators act upon the individual bits of their operands.
They are summarized in the following table:

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

Java Bitwise Logical Operators

The bitwise logical operators are &, |, ^, and ~. The following table shows the outcome
of each operation. In the discussion that follows, keep in mind that the bitwise operators
are applied to each individual bit within each operand.

ABA | BA & BA ^ B~A
000001
101010
011011
111100

Bitwise NOT

Bitwise NOT is also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of its operand.

For example, the number 42, which has the following bit pattern:

00101010

becomes

11010101

after the NOT operator is applied.

Bitwise AND

The AND operator, &, produces a 1 bit if both operands are also 1.

A zero is produced in all other cases. Here is an example:

 00101010      42
&00001111      15
--------------
00001010       10

The Bitwise OR

The OR operator, |, combines bits such that if either of the bits in the operands is a 1, then the resultant bit is a 1, as shown here:

  00101010    42
| 00001111    15
--------------
  00101111    47

Bitwise XOR

The XOR operator, ^, combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero.

The following example shows the effect of the ^.

The bit pattern of 42 is inverted wherever the second operand has a 1 bit.

Wherever the second operand has a 0 bit, the first operand is unchanged.

 00101010    42
^00001111    15
-------------
 00100101    37

Example

The following program demonstrates the bitwise logical operators:

package JAVA.Basic;


public class BitLogic {
    public static void main(String args[]) {
        String binary[] = { 
                "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
                "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
        };
        int a = 3; // 0 + 2 + 1 or 0011 in binary
        int b = 6; // 4 + 2 + 0 or 0110 in binary
        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 = " + binary[a]);
        System.out.println("        b = " + binary[b]);
        System.out.println("      a|b = " + binary[c]);
        System.out.println("      a&b = " + binary[d]);
        System.out.println("      a^b = " + binary[e]);
        System.out.println("~a&b|a&~b = " + binary[f]);
        System.out.println("       ~a = " + binary[g]);
    }
}

Here is the output from this program:

In this example, a and b have bit patterns that present all four possibilities for two binary digits: 0-0, 0-1, 1-0, and 1-1.

You can see how the | and & operate on each bit by the results in c and d.

The values assigned to e and f are the same and illustrate how the ^ works.

The string array named binary holds the human-readable, binary representation of the numbers 0 through 15.

In this example, the array is indexed to show the binary representation of each result.

The array is constructed such that the correct string representation of a binary value n is stored in binary[n].

The value of ~a is ANDed with 0x0f (0000 1111 in binary) in order to reduce its value to less than 16, so it can be printed by use of the binary array.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Arithmetic Operators


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


2 Comments

Leave a Reply

Your email address will not be published.