Ten Ways to Check if an Integer Is a Power Of Two in C

To write a program to check if an integer is a power of two, you could follow two basic strategies: check the number based on its decimal value, or check it based on its binary representation. The former approach is more human-friendly but generally less efficient; the latter approach is more machine-friendly but generally more efficient. We will explore both approaches, comparing ten different but equivalent C functions.

Continue reading “Ten Ways to Check if an Integer Is a Power Of Two in C”

What Powers of Two Look Like Inside a Computer

A power of two, when expressed as a binary number, is easy to spot: it has one, and only one, 1 bit. For example, 1000, 10, and 0.001 are powers of two. Inside a computer, however, numbers are more generally represented in binary code, not as “pure” binary numbers. As a result, you may not be able to look at the binary representation of a number and tell at a glance whether it’s a power of two or not; it depends on how it’s encoded.

Continue reading “What Powers of Two Look Like Inside a Computer”

Copyright © 2008-2023 Exploring Binary

Privacy policy

Powered by WordPress

css.php