TIW: Bitwise
Bitwise operations are black magic. It is so simple but with them you can do things that you never thought would be so easy. For those who have never seen them, bitwise operations operate on one or two integer type variables, and treat them as an array of booleans. Each operation acts on the elements individually. Let’s see what bitwise operations we have:
- And: a & b. 0011 & 0101 = 0001 in binary, so 3 & 5 = 1.
- Or: a b. 0011 0101 = 0111 in binary, so 3 & 5 = 7.
- Exclusive or: a ^ b. 0011 ^ 0101 = 0110 in binary, so 3 & 5 = 6.
- Not: ~a. ~00001111 = 11110000. Depending on the number of bits of your integer data type, the values could vary.
- Shift left and right: 001011 « 1 = 010110, 001011 » 2 = 000010. It is essentially multiplying or dividing by 2 to the power of k.
Applications are sort of miscellaneous and interesting. First let me go through some common routines, then I will go over some problems.
Taking a bit at a certain index
int bitAtIndex(int v, int k) {
return (v >> k) & 1;
}
Shift the bit you want to the least significant bit, then and it with 1 to get rid of all the other higher bits.
Clearing and setting a bit
void clearBit(int& v, int k) {
v &= ~(1 << k);
}
void setBit(int& v, int k) {
v |= 1 << k;
}
This idea is called masking: create a mask, apply it on the number by either or-ing or and-ing.
Getting the lowest 1 bit
int lowBit(int v) {
return v & (-v);
}
This is sort of tricky. Let’s walk through it. Say our number is 00011000. In two’s complement, the negative number of v is obtained by 1+(~v). So the negative of 00011000 is 11100111 plus 1, which is 11101000. Taking the and result with 00011000 will yield 00001000, which is the lowest bit we want. The way to understand it is that the tail of our input number v, defined as the pattern 100000… at the end, will remain the same after taking the negative. Everything to the left of the tail will be flipped. Therefore taking the and result will yield the lowest bit that is a 1. This is particularly useful for Binary Indexed Tree, which I will talk about in a coming post.
Here’s the most cliched problem.
Single Number
Given an array, find the only number that appeared once. This problem is called Single Number for a reason: imagine it’s Christmas time and you’re out there on the streets alone, and everyone you see is with their significant others. Probably how they got the problem idea. O(n) time with O(1) space. To solve this problem, we need to find an operation that acting on the same number twice will yield the identity function, i.e. f(f(a, b), b) = a. This function had better be commutative and associative, so we can do it in any order, and cancel out all the pairs. Obviously +, -, *, / don’t meet the requirements. The answer is exclusive or. The ideas are: a^a = 0, a^0 = a. Therefore 3^2^1^2^3 = 2^2^3^3^1 = 0^1 = 1, exclusive or-ing all numbers gives you the answer.
int singleNumber(vector<int>& nums) {
int ans = 0;
for (int x : nums)
ans ^= x;
return ans;
}
One more remark: it’s possible to extend this algorithm to find the only number that appears once, given all other numbers appear n times for n ≥ 2. What we want to accomplish essentially is to create a commutative function that goes to identity after n operations. The function is this: maintain a vector of integers and size 32, counting the numbers of 1 at each bit mod n. It is easy to see after n times, the counts will be either 0 or n, both equal to 0 mod n. So we will end up with the answer. Our solution above is just a degenerate case when n = 2, so a vector of int mod 2 can be replaced by simply an integer, and modular addition can be replaced by exclusive or. Single Number II is the problem for n = 3.
int singleNumber(vector<int>& nums) {
vector<int> c(32);
for (int x : nums)
for (int j = 0; j < 32; j++)
c[j] = (c[j]+((x>>j)&1))%3;
int ans = 0;
for (int i = 0; i < 32; i++)
ans |= c[i] << i;
return ans;
}
For the people who have never seen bitwise, this is sort of complicated. You can see taking a bit on line 5 and setting a bit on line 8.
Generating the power set
Given a set s, the power set is the set of all subsets of s. For example if s = {1, 2, 3}, P(s) = { {}, {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}}. Here is one possible implementation using bitwise and:
vector<vector<int> > powerSet(vector<int>& s) {
vector<vector<int> > ans;
for (int i = 0; i < (1 << s.size()); i++) {
ans.push_back({});
for (int j = 0; j < s.size(); j++)
if (i & (1 << j))
ans.back().push_back(s[j]);
}
return ans;
}
The key idea is the outer loop of i. Say for s = {1, 2, 3}, i will loop from 0 to 7, or 000 to 111 in binary. We will have all the bit patterns in that case: 000, 001, 010, 011, 100, 101, 110, 111. Now for each number, each bit indicates whether we include a certain element of the original set. By looping through these bits, we can create each subset and generate the power set.
Counting the number of 1 bits
Given a number v, count how many bits are 1. There are different ways to do it, two will be shown below.
int countBits(unsigned int v) {
int ans = 0;
for (int i = 0; i < 32; i++)
if ((v >> i) & 1)
ans++;
return ans;
}
int countBits(unsigned int v) {
int ans = 0;
for (; v > 0; v -= v & (-v), ans++);
return ans;
}
The second method uses the low bit function, removing the lowest bit every time. I suppose it is more efficient, but it probably doesn’t make a real difference.
Swapping two numbers without space
void swapNumbers(int& a, int& b) {
a = a^b; // a ^= b;
b = a^b; // b ^= a;
a = a^b; // a ^= b;
}
Not very useful, but good to know.
Sum of Two Integers
Here’s a brainteaser: add two numbers, but the code cannot have any + or -. The idea of the code: calculate the carry bits and the addition without carry bits, then add the two results together. The base case is when one number becomes 0. There will not be an infinite loop because the number of trailing zeros of the carry bits must increase each time in a recursion. In the following code, if any of the numbers is 0, the sum is equal to bitwise or. Otherwise, the bits without carry will be a exclusive or b, and the carry bits will be where both a and b are 1, shifted to the left by 1.
int getSum(int a, int b) {
return (a == 0 || b == 0)? a | b : getSum(a ^ b, (a & b) << 1);
}
Sudoku Solver by Lee Hsien Loong
This code is written by the prime minister of Singapore. It is written in pure C, so it is kind of hard to read. He used a lot of bitwise operators in this code. I read it two years ago, and I don’t want to spend the time understanding everything again, so my short explanation might be faulty. The algorithm is not tricky, as he simply picks a grid with the smallest number of choices, and tries everything recursively (line 180). To speed things up, he used integers as boolean arrays to indicate what numbers are still available for a certain row, column or 3x3 block. Therefore to get the possible placements at a certain grid simply requires taking the bitwise and result (line 171). To use one possible result, he took the lowest bit (line 173). Another trick to reduce the runtime is to pick the grid with the fewest possible choices (lines 162, 163, 188 I assume). He also pre-computed some functions into arrays to avoid repeated work. Most of these are optimizations that reduce the time constant, replacing one O(1) operation with another O(1) operation. Tricky and efficient, but also with reduced readability, in my opinion.
Anyway that’s a lot already; I will split the Binary Indexed Tree part in a separate post. Surely these are mostly brainteasers, but some interviewers do like them, and for some lower level (closer to hardware) jobs they are quite important.