Java Bitwise Operators

Java Bitwise Operators operate on individual bits of the operands. Let us learn about the type of Bitwise operators in Java. In this lesson, our example will have three variables,

Bitwise Operators in Java are the following:

  • Bitwise NOT,
  • Bitwise AND,
  • Bitwise OR,
  • Bitwise X-OR
  • Bitwise Right Shift Operator,
  • Bitwise Right Shift Phil Zero Operator,
  • Bitwise Left Shift Operator,
  • Bitwise AND Assignment,
  • Bitwise OR Assignment,
  • Bitwise X-OR Assignment,
  • Bitwise Right Shift Assignment Operator,
  • Bitwise Right Shift Phil Zero Assignment Operator,
  • Bitwise Left Shift Assignment Operator

Bitwise NOT

The Bitwise NOT operator inverts all the bits of its operand.

Symbol

Example

Let’s say a = 6, then

Output

The output is 1,

Showing the above, with bits and the same result is visible,

Bitwise Operators

Bitwise AND

The Bitwise AND operator gives 1, if both the operands are 1.

Symbol

Example

Output

The output is 6,

Showing the above, with bits and the same result is visible,

Java Bitwise AND Operator

Bitwise OR

The Bitwise OR operator gives 1, if either of the bit is 1.

Symbol

Example

Output

The output is 7,

Showing the above, with bits and the same result is visible,

Java Bitwise OR Operator

Bitwise X-OR

The Bitwise X-OR operator gives 1, if exactly one operand is 1.

Symbol

Example

Output

The output is 1,

Showing the above, with bits and the same result is visible,

Java Bitwise XOR Operator

Bitwise Right Shift Operator

The Bitwise Right Shift operator shifts bit to the right specified number of times.

Symbol

Example

Output

The output is 3,

Showing the above, with bits and the same result is visible. It shifts once,

Java Bitwise Right Shift Operator

Bitwise Right Shift Phil Zero Operator/ Unsigned Right Shift

The Bitwise Right Shift Zero operator does not preserve sign of original number and fills the new place with zero,

Symbol

Example

Output

The output is 1,

Bitwise Left Shift Operator

The Bitwise Left Shift operator shifts the bit to the left specified number of times in the bit sequence.

Symbol

Example

Output

The output is 4,

Showing the above, with bits and the same result is visible. It shifts once,

Java Bitwise Left Shift Operator

Bitwise AND Assignment

The Bitwise AND Assignment operator performs Bitwise AND and assigns the result to the variable. The Bitwise AND operator gives 1, if both the operands are 1.

Example

Output

The output is 7,

Bitwise OR Assignment

The Bitwise OR Assignment operator performs a Bitwise OR and assigns the result to the variable. The Bitwise OR operator gives 1, if either of the bit is 1.

Example

Output

The value of the result is in the variable a. The output is 7,

Bitwise X-OR Assignment

The Bitwise X-OR Assignment operator performs Bitwise X-OR and assigns the result to the variable. The Bitwise X-OR operator gives 1, if exactly one operand is 1.

Example

Output

The value of the result is in the variable a. The output is 2,

Right Shift Assignment

The Bitwise Right Shift Assignment operator performs Bitwise Right Shift and assigns the result to the variable. The Bitwise Right Shift operator shifts bit to the right specified number of times.

Example

Output

The value of the result is in the variable a. The output is 3,

Right Shift Phil Zero Assignment

The Bitwise Right Shift Phil Zero Assignment operator performs right shift phil zero and assigns the result to the variable.

The Bitwise Right Shift Zero operator does not preserve sign of original number and fills the new place with zero,

Example

Output

The value of the result is in the variable a. It shifts twice.

The output is 1,

Left Shift Assignment

The Bitwise Left Shift Assignment operator performs Bitwise Left Shift and assigns the result to the variable.

The Bitwise Left Shift operator shifts the bit to the left specified number of times in the bit sequence.

Example

Output

The value of the result is in the variable a.

The output is 4,

Java Virtual Machine (JVM)
Java Variables
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading