Bitwise Operators Pt. II: Use Cases

Jacob Lozano
3 min readJan 11, 2021

Luckily with today’s computing power, the necessity of bit manipulation isn’t as prevalent as it once was as programmers no longer need to account for every literal bit. Still, these are good things to have in your tool belt, especially in low memory environments like in PLCs.

A common example case is determining if a number is a power of two. Visually, this is very easy to inspect. If the bit has a single 1, it is a power of two. With bitwise operators, it takes a bit more work:

isPowerOfTwo(x) 
{
return x && (!(x & (x - 1)));
}

--

--