Java supports multiple bit operations like :

- OR ( | )
- AND ( & )
- XOR ( ^ )
- Left Shift ( << )
- Right Shift ( >> )
- Unsigned Right Shift ( >>> )
- Complement ( ! )

**Bitwise OR ( | )**

OR operation follows the following truth table:

A | B | A | B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

So for example we have two integers A = 8 and B = 7

Operation | Binary | Decimal | ||||
---|---|---|---|---|---|---|

0 | 1 | 0 | 0 | 0 | 8 | |

0 | 0 | 1 | 1 | 1 | 7 | |

OR | 0 | 1 | 1 | 1 | 1 | 15 |

After OR operation, result will be 0 1 1 1 1 which is 15

Bitwise AND ( & )

AND operation follows the following truth table.

A | B | A & B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

So, in the same example shared above , let us consider two integers A = 8 and B = 7

Operation | Binary | Decimal | ||||
---|---|---|---|---|---|---|

0 | 1 | 0 | 0 | 0 | 8 | |

0 | 0 | 1 | 1 | 1 | 7 | |

AND | 0 | 0 | 0 | 0 | 0 | 0 |

After AND operation , result will be 0 0 0 0 0

**Bitwise XOR ( ^ )**

XOR (Exclusive OR) operation follows the following truth table.

A | B | A ^ B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

As shown above, the output is only one when either of the two inputs are one. If both or none are one , then output is zero.

In this case, lets change the example a bit to consider the scenario when both the bits are one. Let us consider two integers A = 8 and B = 15

Operation | Binary | Decimal | ||||
---|---|---|---|---|---|---|

0 | 1 | 0 | 0 | 0 | 8 | |

0 | 1 | 1 | 1 | 1 | 15 | |

XOR | 0 | 0 | 1 | 1 | 1 | 7 |

After AND operation , result will be 0 0 1 1 1 which is 7 in decimal.

**Left Shift ( << )**

Left shift operation allows you to shift bits to the left

This is arithmetic shift left which will take care of the signed bit. Lets consider the below examples where first we will left shift 4 bit (positive and negative number)

Example 1: (Positive Decimal)

Example 2: (Negative Decimal)

As you can see in above examples, signed but is preserved and all bits have shifted left.

**Right Shift ( >> )**

Right shift operation allows you to shift bits to the right

This is arithmetic shift right which will again take care of the signed bit as in the left bit shift shown above. Below examples will give further understanding about the same.

Example 1: (Positive Decimal)

Example 2: (Negative Decimal)

As expected, bits are shifted right keeping the signed bit preserved.

**Unsigned right Shift ( >> )**

Unsigned right shift operation allows you to shift bits to the right

This is logical shift right which will not take care of the signed bit. Lets understand it through the same example as of arithmetic shift right.

Example 1: (Positive Decimal)

Example 2: (Negative Decimal)

As clear from above, difference in arithmetic and logic shift right only differs when input is negative. In this case, signed bit is not preserved and hence the output is different.

**Compliment ( ~ )**

Compliment operation follows the following truth table.

A | ~ A |
---|---|

0 | 1 |

1 | 0 |

As shown above, it will just flip all Ones to Zeros and all Zeros to Ones

In this case, lets take compliment of decimal number 3

Operation | Binary | Decimal | ||||
---|---|---|---|---|---|---|

0 | 0 | 0 | 1 | 1 | 3 | |

Compliment | 1 | 1 | 1 | 0 | 0 | 28 |

After compliment operation , result will be 1 1 1 0 0 which is 28 in decimal.