An Illustrated Guide to Bitwise Operators

Jerry An
Jerry An
Feb 7 · 4 min read

Bitwise operations are hard to understand for some beginning programmers. So this post is to explain them easily and unforgettably.

What’s a bit and bit string?

A bit is the smallest unit of information that can be stored or manipulated on a computer. It consists of a single Boolean value (0 or 1).

A bit string is a sequence of bits. It can be used to represent sets or to manipulate binary data.

Bitwise Operations

Bitwise AND (&)

The bitwise AND operator (&) returns a 1 in each bit position for which the corresponding bits of both operands (input and mask) are 1s.

Two principles:

a. Y AND 1 = Y

b. Y AND 0 = 0

Therefore, to turn certain bits off, AND can be used with a 0 .To leave a bit unchanged, AND is used with a 1

Example: Masking on the higher bits (bits 4,5,6) the lower bits (bits 0,1,2, 3) unchanged.

Bitwise And &

Bitwise OR (|)

The bitwise OR operator (|) returns a 1 in each bit position for which the corresponding bits of either or both operands(input and mask) are 1s.

Principles:

a. Y OR 1 = 1

b. Y OR 0 = Y

Therefore, to make certain bits on, OR can be used with a 1 .To leave a bit unchanged, OR is used with a 0.

Example: Masking on the lower bits (bits 0,1, 2, 3) the higher bits (bits 4, 5, 6) unchanged.

Bitwise Or |

Bitwise exclusive OR (^)

The bitwise XOR operator (^) returns a 1 in each bit position for which the corresponding bits of either but not both operands (input and mask) are 1s

In another word, Bitwise exclusive OR sets 1 in each position where its operands have different bits, and 0 if they’re the same.

Two principles:

a. Y ^ 0 = Y

b. Y ^ a = 0

Left shift and Right shift

A bit shift moves each digit in a number’s binary representation left or right.

Left shift and Right shift

When shifting left, the most-significant bit is lost, and a 00 bit is inserted on the other end. When shifting right with a logical right shift, the least-significant bit is lost and a 00 is inserted on the other end.

Bitmask Functions

How do you get, set, clear, and toggle a single bit?

Getting a bit

To querying the status of a bit, shift the number n to the right, then bitwise AND with1

(number >> n) & 1
querying the status of a bit

Setting a bit

To turn certain bits on, the bitwise OR an operation can be used

number |= 1 << n

Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

number &= ~(1 << n)

Toggle a bit

The XOR operator (^) can be used to toggle a bit.

number ^= 1 << n

Bit manipulation Problems

After having an understanding of bit operations, now it’s time for practice.

  1. Number of Steps to Reduce a Number to Zero
  2. XOR Operation in an Array
  3. Number of 1 Bit (HammingWeight) (**)
  4. Hamming Distance

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Jerry An

Written by

Jerry An

Developer in China, AI and machine learning enthusiast

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Jerry An

Written by

Jerry An

Developer in China, AI and machine learning enthusiast

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store