Principle#
The binary representations we use now are very simple: 1, 2, 4, 8, 16 ······
A close observation reveals that in a string of binary numbers, if only one 1 appears, it is a power of 2.
Code#
template <typename T, class = std::enable_if_t<std::is_integral_v<T>>>
inline bool power2_1(T v)
{
return v && !(v & (v - 1));
}
Or
template <typename T, class = std::enable_if_t<std::is_integral_v<T>>>
inline bool power2_2(T v)
{
return v && (v & -v) == v;
}
Principle Analysis#
Method One#
Since a power of 2 has only one 1, we only need to remove the last 1 and check if it equals 0.
v & (v - 1);
The above code can remove the lowest bit 1, and the principle is simple: subtracting 1 will turn the lowest bit 1 into 0 and produce a 1 in a lower bit, while other bits remain unchanged. After ANDing with itself, these 1s and the previous lowest bit 1 will be cleared.
However, 0 is a special case, so we need to exclude it:
v && !(v & (v - 1));
Method Two#
Method two is similar to method one. First, we need to know what v & -v
does. v & -v
actually retrieves the index of the first 1 bit from low to high in a binary number. For example, for 111, the complement is 001, and 111 & 001 = 001; for 110, the complement is 010, and 110 & 010 = 010;
It is evident that if the bit index of a number equals itself, then it is a power of 2.
Benchmark#
#include "benchmark/benchmark.h"
template<typename T, class = std::enable_if_t<std::is_integral_v<T>>>
inline bool power2_1(T v) {
return v && !(v & (v - 1));
}
template<typename T, class = std::enable_if_t<std::is_integral_v<T>>>
inline bool power2_2(T v) {
return v && ((v & -v) == v);
}
static void BM_power2_1(benchmark::State &state) {
for (auto _: state) {
benchmark::DoNotOptimize(power2_1(state.range(0)));
}
}
static void BM_power2_2(benchmark::State &state) {
for (auto _: state) {
benchmark::DoNotOptimize(power2_2(state.range(0)));
}
}
BENCHMARK(BM_power2_1)->RangeMultiplier(32)->Range(INT64_MIN, INT64_MAX);
BENCHMARK(BM_power2_2)->RangeMultiplier(32)->Range(INT64_MIN, INT64_MAX);
BENCHMARK_MAIN();
Below are the results obtained using MacBook Air (M1, 2020) and Apple clang 13.1.6
/Users/hominsu/CLionProjects/bit-hacks-bench/cmake-build-release-appleclang/bench/power2
Unable to determine clock rate from sysctl: hw.cpufrequency: No such file or directory
2022-03-26T13:24:41+08:00
Running /Users/hominsu/CLionProjects/bit-hacks-bench/cmake-build-release-appleclang/bench/power2
Run on (8 X 24.0935 MHz CPU s)
CPU Caches:
L1 Data 64 KiB (x8)
L1 Instruction 128 KiB (x8)
L2 Unified 4096 KiB (x2)
Load Average: 1.38, 1.45, 1.71
---------------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------------
BM_power2_1/-9223372036854775808 0.443 ns 0.443 ns 1000000000
BM_power2_1/-1152921504606846976 0.443 ns 0.443 ns 1000000000
BM_power2_1/-36028797018963968 0.443 ns 0.443 ns 1000000000
BM_power2_1/-1125899906842624 0.443 ns 0.443 ns 1000000000
BM_power2_1/-35184372088832 0.443 ns 0.443 ns 1000000000
BM_power2_1/-1099511627776 0.444 ns 0.444 ns 1000000000
BM_power2_1/-34359738368 0.443 ns 0.443 ns 1000000000
BM_power2_1/-1073741824 0.444 ns 0.444 ns 1000000000
BM_power2_1/-33554432 0.444 ns 0.444 ns 1000000000
BM_power2_1/-1048576 0.444 ns 0.444 ns 1000000000
BM_power2_1/-32768 0.443 ns 0.443 ns 1000000000
BM_power2_1/-1024 0.443 ns 0.443 ns 1000000000
BM_power2_1/-32 0.444 ns 0.444 ns 1000000000
BM_power2_1/-1 0.444 ns 0.444 ns 1000000000
BM_power2_1/0 0.314 ns 0.314 ns 1000000000
BM_power2_1/1 0.444 ns 0.443 ns 1000000000
BM_power2_1/32 0.444 ns 0.444 ns 1000000000
BM_power2_1/1024 0.443 ns 0.443 ns 1000000000
BM_power2_1/32768 0.443 ns 0.443 ns 1000000000
BM_power2_1/1048576 0.443 ns 0.443 ns 1000000000
BM_power2_1/33554432 0.446 ns 0.446 ns 1000000000
BM_power2_1/1073741824 0.443 ns 0.443 ns 1000000000
BM_power2_1/34359738368 0.443 ns 0.443 ns 1000000000
BM_power2_1/1099511627776 0.444 ns 0.444 ns 1000000000
BM_power2_1/35184372088832 0.443 ns 0.443 ns 1000000000
BM_power2_1/1125899906842624 0.444 ns 0.444 ns 1000000000
BM_power2_1/36028797018963968 0.443 ns 0.443 ns 1000000000
BM_power2_1/1152921504606846976 0.443 ns 0.443 ns 1000000000
BM_power2_1/9223372036854775807 0.444 ns 0.444 ns 1000000000
BM_power2_2/-9223372036854775808 0.443 ns 0.443 ns 1000000000
BM_power2_2/-1152921504606846976 0.443 ns 0.443 ns 1000000000
BM_power2_2/-36028797018963968 0.444 ns 0.444 ns 1000000000
BM_power2_2/-1125899906842624 0.444 ns 0.444 ns 1000000000
BM_power2_2/-35184372088832 0.443 ns 0.443 ns 1000000000
BM_power2_2/-1099511627776 0.443 ns 0.443 ns 1000000000
BM_power2_2/-34359738368 0.444 ns 0.444 ns 1000000000
BM_power2_2/-1073741824 0.444 ns 0.444 ns 1000000000
BM_power2_2/-33554432 0.443 ns 0.443 ns 1000000000
BM_power2_2/-1048576 0.444 ns 0.444 ns 1000000000
BM_power2_2/-32768 0.444 ns 0.444 ns 1000000000
BM_power2_2/-1024 0.445 ns 0.445 ns 1000000000
BM_power2_2/-32 0.444 ns 0.444 ns 1000000000
BM_power2_2/-1 0.443 ns 0.443 ns 1000000000
BM_power2_2/0 0.313 ns 0.313 ns 1000000000
BM_power2_2/1 0.443 ns 0.443 ns 1000000000
BM_power2_2/32 0.444 ns 0.444 ns 1000000000
BM_power2_2/1024 0.444 ns 0.443 ns 1000000000
BM_power2_2/32768 0.443 ns 0.443 ns 1000000000
BM_power2_2/1048576 0.443 ns 0.443 ns 1000000000
BM_power2_2/33554432 0.444 ns 0.444 ns 1000000000
BM_power2_2/1073741824 0.443 ns 0.443 ns 1000000000
BM_power2_2/34359738368 0.443 ns 0.443 ns 1000000000
BM_power2_2/1099511627776 0.443 ns 0.443 ns 1000000000
BM_power2_2/35184372088832 0.443 ns 0.443 ns 1000000000
BM_power2_2/1125899906842624 0.444 ns 0.444 ns 1000000000
BM_power2_2/36028797018963968 0.445 ns 0.445 ns 1000000000
BM_power2_2/1152921504606846976 0.444 ns 0.444 ns 1000000000
BM_power2_2/9223372036854775807 0.450 ns 0.449 ns 1000000000
Below are the results obtained using i5-9500 and gcc 8.5.0 (Red Hat 8.5.0-10) on CentOS-8-Stream
/tmp/tmp.CtmwmpTLjC/cmake-build-release-1104/bench/power2
2022-03-26T13:30:11+08:00
Running /tmp/tmp.CtmwmpTLjC/cmake-build-release-1104/bench/power2
Run on (6 X 4099.87 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x6)
L1 Instruction 32 KiB (x6)
L2 Unified 256 KiB (x6)
L3 Unified 9216 KiB (x1)
Load Average: 3.17, 1.60, 1.17
---------------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------------
BM_power2_1/-9223372036854775808 0.487 ns 0.487 ns 1000000000
BM_power2_1/-1152921504606846976 0.496 ns 0.495 ns 1000000000
BM_power2_1/-36028797018963968 0.490 ns 0.489 ns 1000000000
BM_power2_1/-1125899906842624 0.489 ns 0.489 ns 1000000000
BM_power2_1/-35184372088832 0.485 ns 0.484 ns 1000000000
BM_power2_1/-1099511627776 0.493 ns 0.492 ns 1000000000
BM_power2_1/-34359738368 0.488 ns 0.488 ns 1000000000
BM_power2_1/-1073741824 0.491 ns 0.490 ns 1000000000
BM_power2_1/-33554432 0.489 ns 0.488 ns 1000000000
BM_power2_1/-1048576 0.496 ns 0.495 ns 1000000000
BM_power2_1/-32768 0.491 ns 0.490 ns 1000000000
BM_power2_1/-1024 0.491 ns 0.490 ns 1000000000
BM_power2_1/-32 0.484 ns 0.484 ns 1000000000
BM_power2_1/-1 0.495 ns 0.494 ns 1000000000
BM_power2_1/0 0.886 ns 0.885 ns 788464796
BM_power2_1/1 0.486 ns 0.486 ns 1000000000
BM_power2_1/32 0.491 ns 0.490 ns 1000000000
BM_power2_1/1024 0.489 ns 0.489 ns 1000000000
BM_power2_1/32768 0.491 ns 0.491 ns 1000000000
BM_power2_1/1048576 0.491 ns 0.490 ns 1000000000
BM_power2_1/33554432 0.494 ns 0.493 ns 1000000000
BM_power2_1/1073741824 0.484 ns 0.484 ns 1000000000
BM_power2_1/34359738368 0.492 ns 0.491 ns 1000000000
BM_power2_1/1099511627776 0.491 ns 0.490 ns 1000000000
BM_power2_1/35184372088832 0.495 ns 0.495 ns 1000000000
BM_power2_1/1125899906842624 0.484 ns 0.483 ns 1000000000
BM_power2_1/36028797018963968 0.493 ns 0.492 ns 1000000000
BM_power2_1/1152921504606846976 0.491 ns 0.490 ns 1000000000
BM_power2_1/9223372036854775807 0.496 ns 0.495 ns 1000000000
BM_power2_2/-9223372036854775808 0.552 ns 0.551 ns 1000000000
BM_power2_2/-1152921504606846976 0.552 ns 0.552 ns 1000000000
BM_power2_2/-36028797018963968 0.561 ns 0.560 ns 1000000000
BM_power2_2/-1125899906842624 0.546 ns 0.546 ns 1000000000
BM_power2_2/-35184372088832 0.551 ns 0.550 ns 1000000000
BM_power2_2/-1099511627776 0.553 ns 0.553 ns 1000000000
BM_power2_2/-34359738368 0.552 ns 0.551 ns 1000000000
BM_power2_2/-1073741824 0.552 ns 0.552 ns 1000000000
BM_power2_2/-33554432 0.553 ns 0.552 ns 1000000000
BM_power2_2/-1048576 0.553 ns 0.552 ns 1000000000
BM_power2_2/-32768 0.545 ns 0.545 ns 1000000000
BM_power2_2/-1024 0.554 ns 0.553 ns 1000000000
BM_power2_2/-32 0.548 ns 0.547 ns 1000000000
BM_power2_2/-1 0.546 ns 0.546 ns 1000000000
BM_power2_2/0 0.493 ns 0.493 ns 1000000000
BM_power2_2/1 0.553 ns 0.553 ns 1000000000
BM_power2_2/32 0.554 ns 0.553 ns 1000000000
BM_power2_2/1024 0.545 ns 0.544 ns 1000000000
BM_power2_2/32768 0.555 ns 0.555 ns 1000000000
BM_power2_2/1048576 0.550 ns 0.549 ns 1000000000
BM_power2_2/33554432 0.550 ns 0.549 ns 1000000000
BM_power2_2/1073741824 0.555 ns 0.554 ns 1000000000
BM_power2_2/34359738368 0.551 ns 0.550 ns 1000000000
BM_power2_2/1099511627776 0.553 ns 0.553 ns 1000000000
BM_power2_2/35184372088832 0.553 ns 0.552 ns 1000000000
BM_power2_2/1125899906842624 0.552 ns 0.552 ns 1000000000
BM_power2_2/36028797018963968 0.552 ns 0.552 ns 1000000000
BM_power2_2/1152921504606846976 0.551 ns 0.551 ns 1000000000
BM_power2_2/9223372036854775807 0.554 ns 0.553 ns 1000000000