Any power of 2 minus 1 is all ones: (2 N – 1 = 111….b)
2 = 2^1. 2-1 = 1 (1b)
4 = 2^2. 4-1 = 3 (11b)
8 = 2^3. 8-1 = 7 (111b)
Take 8 for example. 1000 & 0111 = 0000
So that expression tests if a number is NOT a power of 2.
Related Contents:
- Explain this snippet which finds the maximum of two integers without using if-else or any other comparison operator?
- Compute fast log base 2 ceiling
- What is the behavior of integer division?
- The most efficient way to implement an integer based power function pow(int, int)
- What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?
- How does C compute sin() and other math functions?
- Efficient Algorithm for Bit Reversal (from MSB->LSB to LSB->MSB) in C
- Arithmetic bit-shift on a signed integer
- Power by squaring for negative exponents
- How can I multiply and divide using only bit shifting and adding?
- What is Bit Masking?
- What’s the difference between “mod” and “remainder”?
- Are the shift operators () arithmetic or logical in C?
- What is CHAR_BIT?
- Rounding up to next power of 2
- Catch and compute overflow during multiplication of two large integers
- Find most significant bit (left-most) that is set in a bit array
- How do I determine the number of digits of an integer in C?
- Undefined reference to `sin` [duplicate]
- Extracting bits with a single multiplication
- SIMD signed with unsigned multiplication for 64-bit * 64-bit to 128-bit
- Divide a number by 3 without using *, /, +, -, % operators
- Mapping a numeric range onto another
- Bitwise operators and “endianness”
- Bitfield manipulation in C
- Bit twiddling: which bit is set?
- Fastest way to clamp a real (fixed/floating point) value?
- Bitshift and integer promotion?
- How to use nan and inf in C?
- “undefined reference to `pow'” even with math.h and the library link -lm [duplicate]
- Using M_PI with C89 standard
- Representing integers in doubles
- Why does gdb evaluate sqrt(3) to 0?
- Gaussian elimination without result for acceleration
- Encoding int value as an IEEE-754 float (binary32)
- What does the statement if (counter & (1
- Unexpected C/C++ bitwise shift operators outcome
- Are floating point operations in C associative?
- Finding consecutive bit string of 1 or 0
- C: How to wrap a float to the interval [-pi, pi)
- C: how to break apart a multi digit number into separate variables?
- How to concatenate two integers in C
- Implementing Logical Right Shift in C
- What’s the quickest way to compute log2 of an integer in C#?
- Order of fields when using a bit field in C
- C reverse bits in unsigned integer
- warning: left shift count >= width of type
- Algorithm to pick values from array that sum closest to a target value?
- set the m-bit to n-bit [closed]
- Algorithm to generate bit mask