An Illustrated Guide to Bitwise Operators

Jerry An
The Startup
Published in
4 min readFeb 7, 2021

--

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.

--

--