The Bitwise operators in Java programming are used to perform bit operations. All the decimal values will be converted into binary values (sequence of bits i.e 0100, 1100, 1000, 1001 etc) and bitwise operator will work on these bits such as shifting them left to right or converting bit value from 0 to 1 etc. Below table shows the different Bitwise operators and their meaning.

For example, Consider x = 6 and y = 8 and their values in binary form are

x = 0110

y = 1000

Bitwise Operators in Java | Meaning of operators | Examples |
---|---|---|

& | Bitwise AND | X & Y = 0000 |

| | Bitwise OR | X | Y = 1110 |

^ | Bitwise exclusive OR | X ^ Y = 1110 |

~ | Bitwise complement | ~X = 00001001 (Bitwise Not operator will convert all 0 into 1). Remember, Bitwise Compliment of N will be -(N + 1) |

<< | Shift left | X << 1 = 00001100 (Bits will move 1 step left. If we use 2 or 3 then they shift accordingly) |

>> | Shift right | Y >> 1 = 00000100 |

Let us see the Truth Table behind Bitwise Operators in Java Programming Language

x |
y |
x & y |
X | y |
x ^ y |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

## Bitwise operators in Java example

This example helps you to understand the Java Bitwsie Operators practically. This Java program allows the user to enter two integer variables a and b and we are going to use these two variables to show you various Bitwise operators present in Java Programming Language.

**JAVA CODE**

// Bitwise operators in Java Example package JavaOperators; import java.util.Scanner; public class BitwiseOperators { private static Scanner sc; public static void main(String[] args) { int a, b; sc = new Scanner(System.in); System.out.println(" Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt(); System.out.format(" Bitwise AND Operator: %d & %d = %d \n", a, b, a & b); System.out.format(" Bitwise OR Operator: %d | %d = %d \n", a, b, a | b); System.out.format(" Bitwise EXCLUSIVE OR: Operator %d ^ %d = %d \n", a, b, a ^ b); System.out.format(" Bitwise NOT Operator: ~%d = %d \n", a, ~a); System.out.format(" LEFT SHIFT Operator: %d << 1 = %d \n", a, a << 1); System.out.format(" RIGHT SHIFT Operator: %d >> 1 = %d \n", b, b >> 1); } }

**OUTPUT**

**ANALYSIS**

Following statement will ask the user to enter integer values a, b and we are going to assign the user input values to respected variables.

System.out.println(" Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt();

Following System.out.format statements will perform the bitwise operations on a and b and then they will display the output

System.out.format(" Bitwise AND Operator: %d & %d = %d \n", a, b, a & b); System.out.format(" Bitwise OR Operator: %d | %d = %d \n", a, b, a | b); System.out.format(" Bitwise EXCLUSIVE OR: Operator %d ^ %d = %d \n", a, b, a ^ b); System.out.format(" Bitwise NOT Operator: ~%d = %d \n", a, ~a); System.out.format(" LEFT SHIFT Operator: %d << 1 = %d \n", a, a << 1); System.out.format(" RIGHT SHIFT Operator: %d >> 1 = %d \n", b, b >> 1);

In this example ,We assigned the values as a = 12 and b = 25. The binary form of 12 = 00001100 and 25 = 00011001.

Lets see the calculations

Bitwise AND Operation = a & b

00001100 & 00011001 ==> 00001000 = 8

Bitwise OR Operation = a || b

00001100 || 00011001 ==> 00011101 = 29

Bitwise Exclusive OR Operation = a ^ b

00001100 || 00011001 ==> 00010101 = 21

Bitwise Complement Operation = ~a

As we said before, Bitwise Compliment of N will be -(N + 1). It means – (12 + 1) = -13

Left Shift Operation in Bitwise Operator = a << 1

00001100 << 1 = 00011000 = 24

Right Shift Operation in Bitwise Operator = b >> 1

00011001 >> 1 = 00001100 = 12

Thank You for Visiting Our Blog