---
title: "Bitwise AND, OR, XOR, and left/right shift operations."  
description: "Bitwise AND, OR, XOR, and left/right shift operations."  
author: "Ravi Vishwakarma"  
published: 2024-07-17  
updated: 2024-07-17  
canonical: https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations  
category: "java"  
tags: ["java", "javac"]  
reading_time: 4 minutes  

---

# Bitwise AND, OR, XOR, and left/right shift operations.

Bitwise operations in Java are used to manipulate individual bits of integers.

Here's a brief overview of each operation:

**Bitwise AND (**`&`**)**:

- The bitwise AND operator (`&`) compares corresponding bits of two integers. The result is a bit set to `1` if both bits are `1`, otherwise, it is `0`.
- Example: `5 & 3` results in `1` (`0101 & 0011 = 0001`).

**Bitwise OR (**`|`**)**:

- The bitwise OR operator (`|`) compares corresponding bits of two integers. The result is a bit set to `1` if at least one of the bits is `1`.
- Example: `5 | 3` results in `7` (`0101 | 0011 = 0111`).

**Bitwise XOR (**`^`**)**:

- The bitwise XOR operator (`^`) compares corresponding bits of two integers. The result is a bit set to `1` if exactly one of the bits is `1`, but not both.
- Example: `5 ^ 3` results in `6` (`0101 ^ 0011 = 0110`).

**Left Shift (**`<<`**)**:

- The left shift operator (`<<`) shifts the bits of the left-hand operand to the left by several positions specified by the right-hand operand. This operation effectively multiplies the number by `2` to the power of the shift count.
- Example: `5 << 1` results in `10` (`0101 << 1 = 1010`).

**Right Shift (**`>>`**)**:

- The right shift operator (`>>`) shifts the bits of the left-hand operand to the right by several positions specified by the right-hand operand. For positive numbers, this operation effectively divides the number by `2` to the power of the shift count.
- Example: `5 >> 1` results in `2` (`0101 >> 1 = 0010`).

**Unsigned Right Shift (**`>>>`**)**:

- The unsigned right shift operator (`>>>`) shifts zero-filled bits to the right. Regardless of the sign bit, the result of shifting is always non-negative.
- Example: `-5 >>> 1` results in `2147483645` (`11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101` in binary, which equals `2147483645` in decimal).

These bitwise operations are useful for low-level programming, bit manipulation, and optimization in scenarios where performance is critical.

## Answers

### Answer by Ravi Vishwakarma

Bitwise operations in Java are used to manipulate individual bits of integers.

Here's a brief overview of each operation:

**Bitwise AND (**`&`**)**:

- The bitwise AND operator (`&`) compares corresponding bits of two integers. The result is a bit set to `1` if both bits are `1`, otherwise, it is `0`.
- Example: `5 & 3` results in `1` (`0101 & 0011 = 0001`).

**Bitwise OR (**`|`**)**:

- The bitwise OR operator (`|`) compares corresponding bits of two integers. The result is a bit set to `1` if at least one of the bits is `1`.
- Example: `5 | 3` results in `7` (`0101 | 0011 = 0111`).

**Bitwise XOR (**`^`**)**:

- The bitwise XOR operator (`^`) compares corresponding bits of two integers. The result is a bit set to `1` if exactly one of the bits is `1`, but not both.
- Example: `5 ^ 3` results in `6` (`0101 ^ 0011 = 0110`).

**Left Shift (**`<<`**)**:

- The left shift operator (`<<`) shifts the bits of the left-hand operand to the left by several positions specified by the right-hand operand. This operation effectively multiplies the number by `2` to the power of the shift count.
- Example: `5 << 1` results in `10` (`0101 << 1 = 1010`).

**Right Shift (**`>>`**)**:

- The right shift operator (`>>`) shifts the bits of the left-hand operand to the right by several positions specified by the right-hand operand. For positive numbers, this operation effectively divides the number by `2` to the power of the shift count.
- Example: `5 >> 1` results in `2` (`0101 >> 1 = 0010`).

**Unsigned Right Shift (**`>>>`**)**:

- The unsigned right shift operator (`>>>`) shifts zero-filled bits to the right. Regardless of the sign bit, the result of shifting is always non-negative.
- Example: `-5 >>> 1` results in `2147483645` (`11111111111111111111111111111011 >>> 1 = 01111111111111111111111111111101` in binary, which equals `2147483645` in decimal).

These bitwise operations are useful for low-level programming, bit manipulation, and optimization in scenarios where performance is critical.


---

Original Source: https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
