SCIT Logo

SCIT

NUMBER CONVERSIONS TOOL

developed by Hafiz Muhammad Abubakar

A Guide to Bitwise Operations

Bitwise operations are actions that work on the individual bits of a number. They are incredibly fast because they are performed directly by the CPU. While you may not use them every day, they are essential for low-level programming, graphics, encryption, and performance optimization.

Our interactive bit manipulator is a great way to visualize these concepts in action!

The Main Bitwise Operators

Bitwise operations compare two numbers bit by bit. Let's look at the most common ones using the numbers a = 5 (0101) and b = 3 (0011).

AND (&)

The bitwise AND operator returns a 1 in a bit position only if both corresponding bits are 1.

0101  (a = 5)
& 0011 (b = 3)
------
= 0001 (Result = 1)

OR (|)

The bitwise OR operator returns a 1 if at least one of the corresponding bits is 1.

0101  (a = 5)
| 0011 (b = 3)
------
= 0111 (Result = 7)

XOR (^)

The bitwise XOR (exclusive OR) operator returns a 1 only if the corresponding bits are different.

0101  (a = 5)
^ 0011 (b = 3)
------
= 0110 (Result = 6)

NOT (~)

The bitwise NOT operator inverts all the bits of a single number. It flips 1s to 0s and 0s to 1s. This is also known as the complement.

~ 0101  (a = 5)
------
= 1010 (In a 4-bit system, this is 10. In a 32-bit signed integer, the result is -6)

Bit Shifting

Bit shifting moves every bit in a number to the left or right. This is a very fast way to multiply or divide by powers of 2.

Left Shift (<<)

Shifts bits to the left, filling in the empty spaces on the right with zeros. x << n is equivalent to multiplying x by 2n.

5 << 2  (0101 << 2)
= 20 (10100)

Right Shift (>>)

Shifts bits to the right. x >> n is equivalent to dividing x by 2n.

20 >> 2 (10100 >> 2)
= 5 (00101)