Bit manipulation
Binary number
A binary number is a number expressed in the base-2 numeral system or binary numeral system, it is a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one).
We say that a certain bit is set, if it is one, and cleared if it is zero.
The binary number represents the number:
For instance the binary number represents the number :
Computers represent integers as binary numbers. Positive integers (both signed and unsigned) are just represented with their binary digits, and negative signed numbers (which can be positive and negative) are usually represented with the Two's complement.
unsigned int unsigned_number = 13;
assert(unsigned_number == 0b1101);
int positive_signed_number = 13;
assert(positive_signed_number == 0b1101);
int negative_signed_number = -13;
assert(negative_signed_number == 0b1111'1111'1111'1111'1111'1111'1111'0011);CPUs are very fast manipulating those bits with specific operations. For some problems we can take these binary number representations to our advantage, and speed up the execution time. And for some problems (typically in combinatorics or dynamic programming) where we want to track which objects we already picked from a given set of objects, we can just use an large enough integer where each digit represents an object and depending on if we pick or drop the object we set or clear the digit.
Bit operators
All those introduced operators are instant (same speed as an addition) on a CPU for fixed-length integers.
Bitwise operators
- : The bitwise AND operator compares each bit of its first operand with the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- : The bitwise inclusive OR operator compares each bit of its first operand with the corresponding bit of its second operand. If one of the two bits is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- : The bitwise exclusive OR (XOR) operator compares each bit of its first operand with the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- : The bitwise complement (NOT) operator flips each bit of a number, if a bit is set the operator will clear it, if it is cleared the operator sets it.
Examples:
n = 01011000
n-1 = 01010111
--------------------
n & (n-1) = 01010000n = 01011000
n-1 = 01010111
--------------------
n | (n-1) = 01011111n = 01011000
n-1 = 01010111
--------------------
n ^ (n-1) = 00001111n = 01011000
--------------------
~n = 10100111Shift operators
There are two operators for shifting bits.
Shifts a number to the right by removing the last few binary digits of the number. Each shift by one represents an integer division by 2, so a right shift by represents an integer division by .
E.g. which is the same as . For a computer though shifting some bits is a lot faster than doing divisions.
Shifts a number to left by appending zero digits. In similar fashion to a right shift by , a left shift by represents a multiplication by .
E.g. which is the same as .
Notice however that for a fixed-length integer that means dropping the most left digits, and if you shift too much you end up with the number .
Useful tricks
Set/flip/clear a bit
Using bitwise shifts and some basic bitwise operations we can easily set, flip or clear a bit. is a number with only the -th bit set, while is a number with all bits set except the -th bit.
- sets the -th bit in the number
- flips the -th bit in the number
- clears the -th bit in the number
Check if a bit is set
The value of the -th bit can be checked by shifting the number positions to the right, so that the -th bit is at the unit place, after which we can extract it by performing a bitwise & with 1.
bool is_set(unsigned int number, int x) {
return (number >> x) & 1;
}Check if the number is divisible by a power of 2
Using the and operation, we can check if a number is even because if is even, and if is odd. More generally, is divisible by exactly when .
bool isDivisibleByPowerOf2(int n, int k) {
int powerOf2 = 1 << k;
return (n & (powerOf2 - 1)) == 0;
}We can calculate by left shifting 1 by positions. The trick works, because is a number that consists of exactly ones. And a number that is divisible by must have zero digits in those places.
Check if an integer is a power of 2
A power of two is a number that has only a single bit in it (e.g. ), while the predecessor of that number has that digit not set and all the digits after it set (). So the bitwise AND of a number with it's predecessor will always be 0, as they don't have any common digits set. You can easily check that this only happens for the the power of twos and for the number which already has no digit set.
bool isPowerOfTwo(unsigned int n) {
return n && !(n & (n - 1));
}Clear the right-most set bit
The expression can be used to turn off the rightmost set bit of a number . This works because the expression flips all bits after the rightmost set bit of , including the rightmost set bit. So all those digits are different from the original number, and by doing a bitwise AND they are all set to 0, giving you the original number with the rightmost set bit flipped.
For example, consider the number :
n = 00110100
n-1 = 00110011
--------------------
n & (n-1) = 00110000Brian Kernighan's algorithm
We can count the number of bits set with the above expression.
The idea is to consider only the set bits of an integer by turning off its rightmost set bit (after counting it), so the next iteration of the loop considers the Next Rightmost bit.
int countSetBits(int n)
{
int count = 0;
while (n)
{
n = n & (n - 1);
count++;
}
return count;
}Count set bits upto
To count the number of set bits of all numbers upto the number (inclusive), we can run the Brian Kernighan's algorithm on all numbers upto . But this will result in a "Time Limit Exceeded" in contest submissions.
We can use the fact that for numbers upto (i.e. from to ) there are set bits. This can be visualised as follows.
0 -> 0 0 0 0
1 -> 0 0 0 1
2 -> 0 0 1 0
3 -> 0 0 1 1
4 -> 0 1 0 0
5 -> 0 1 0 1
6 -> 0 1 1 0
7 -> 0 1 1 1
8 -> 1 0 0 0We can see that the all the columns except the leftmost have (i.e. ) set bits each, i.e. upto the number , the number of set bits is .
With the new knowledge in hand we can come up with the following algorithm:
- Find the largest exponent such that is lesser than or equal to the given number.
- Calculate the number of set bits from to by using the formula .
- Count the no. of set bits in the most significant bit from to and add it.
- Subtract from and repeat the above steps using the new .
long long popcount_sum(unsigned n) {
long long count = 0;
while (n > 1) {
int x = std::bit_width(n) - 1;
count += (long long)x << (x - 1); // set bits below 2^x
n -= 1u << x;
count += n + 1; // leading bits of 2^x..n
}
return count + n;
}Additional tricks
- clears all trailing ones: .
- sets the last cleared bit: .
- extracts the last set bit: .
Many more can be found in the book Hacker's Delight.
Language and compiler support
C++ supports some of those operations since C++20 via the bit standard library:
has_single_bit: checks if the number is a power of twobit_ceil/bit_floor: round up/down to the next power of tworotl/rotr: rotate the bits in the numbercountl_zero/countr_zero/countl_one/countr_one: count the leading/trailing zeros/onespopcount: count the number of set bits
Additionally, there are also predefined functions in some compilers that help working with bits. E.g. GCC defines a list at Built-in Functions Provided by GCC that also work in older versions of C++:
__builtin_popcount(unsigned int)returns the number of set bits (__builtin_popcount(0b0001'0010'1100) == 4)__builtin_ffs(int)finds the index of the first (most right) set bit (__builtin_ffs(0b0001'0010'1100) == 3)__builtin_clz(unsigned int)the count of leading zeros (__builtin_clz(0b0001'0010'1100) == 23)__builtin_ctz(unsigned int)the count of trailing zeros (__builtin_ctz(0b0001'0010'1100) == 2)__builtin_parity(x)the parity (even or odd) of the number of ones in the bit representation
Note that some of the operations (both the C++20 functions and the Compiler Built-in ones) might be quite slow in GCC if you don't enable a specific compiler target with #pragma GCC target("popcnt").