Introduction
Most of the optimizations that go into topcoder contests are high-level; that is, they affect the algorithm rather than the implementation. however, one of the most useful and valid tive low-level optimizations is bit manipulation, or using the bits of an integer to represent a set. not only does it produce an order-of-magn1_improvement in both speed and size, it can often simplify code at the same time.
I'll start by briefly recapping the basics, before going on to cover more advanced techniques.
The basics
At the heart of bit manipulation are the bit-wise Operators&(And ),|(Or ),~(Not) and^(XOR). The first three you shoshould already be familiar with in their Boolean forms (&&,|And!). As a reminder, here are the truth tables:
A |
B |
! A |
A & B |
A | B |
A ^ B |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
0 |
1 |
1 |
0 |
The bit-wise versions of the operations are the same, doesn't that instead of interpreting their arguments as true or false, they operate on each bit of the arguments. thus, if a is 1010 and B is 1100, then
- A & B = 1000
- A | B = 1110.
- A ^ B = 0110
- ~ A = 11110101 (the number of 1's depends on the type of ).
The other two operators we will need are the Shift OperatorsA <BAnd> B. The former shifts all the bits inATo the leftBPositions; the latter does the same but shifts right. for non-negative values (which are the only ones we're interested in), the newly exposed bits are filled with zeros. you can think of left-shiftingBAs multiplication by 2BAnd right-shifting as integer division by 2B. The most common use for shifting is to access a particle bit, for example,1 <XIs a binary number with bitXSet and the others clear (bits are almost always counted from the right-most/least-significant bit, which is numbered 0 ).
In general, we will use an integer to represent a set on a domain of up to 32 values (or 64, using a 64-bit integer ), with a 1 bit representing a member that is present and a 0 bit one that is absent. then the following operations are quite straightforward, whereAll_bitsIs a number with 1's for all bits corresponding to the elements of the domain:
-
Set Union
- A | B
-
Set intersection
- A & B
-
Set Subtraction
- A &~ B
-
Set Negation
- All_bits ^
-
Set bit
- A | = 1 <bit
-
Clear bit
- A & = ~ (1 <bit)
-
Test bit
- (A & 1 <bit )! = 0
Extracting every last bit
In this section I'll consider the problems of finding the highest and lowest 1 bit in a number. These are basic operations for splitting a set into its elements.
Finding the lowest set bit turns out to be surprisingly easy, with the right combination of bitwise AND Arithmetic Operators. Suppose we wish to find the lowest set bitX(Which is known to be non-zero). If we subtract 1 fromXThen this bit is cleared, but all the other one bits inXRemain set. Thus,X &~ (X-1)Consists of only the lowest set bitX. However, this only tells us the bit value, not the index of the bit.
If we want the index of the highest or lowest Bit, the obvious approach is simply to loop through the BITs (upwards or downwards) until we find one that is set. at first glance this sounds slow, since it does not take advantage of the bit-packing at all. however, if all 2n subsets of the N-element domain are equally likely, then the loop will take only two iterations on average, and this is actually the fastest method.
The 386 introduced CPU instructions for bit scanning: BSF (bit scan forward) and BSR (bit scan reverse). GCC exposes these instructions through the built-in Functions_ Builtin_ctz(Count trailing zeros) and_ Builtin_clz(Count leading zeros). These are the most convenient way to find bit indices for C ++ programmers in topcoder. Be warned though: the return value isUndefinedFor an argument of zero.
Finally, there is a portable method that performs well in cases where the looping solution wowould require specified iterations. use each byte of the 4-or 8-byte integer to index A precomputed 256-entry table that stores the index of the highest (lowest) set bit in that byte. the highest (lowest) Bit Of the integer is then the maximum (minimum) of the table entries. this method is only mentioned for completeness, and the performance gain is unlikely to justify its use in a topcoder match.
Counting out the bits
One can easily check if a number is a power of 2: Clear the lowest 1 bit (see above) and check if the result is 0. however, sometimes it is necessary to know how many bits are set, and this is more difficult.
GCC has a function called_ Builtin_popcountWhich does precisely this. However, unlike_ Builtin_ctz, It does not translate into a hardware instruction (at least on x86 ). instead, it uses a table-based method similar to the one described abve for bit searches. it is nevertheless quite efficient and also extremely convenient.
Users of other versions ages do not have this option (although they cocould re-implement it ). if a number is expected to have very few 1 bits, an alternative is to repeatedly extract the lowest 1 bit and clear it.
All the Subsets
A big advantage of bit manipulation is that it is trivial to iterate over all the subsets of an n-Element Set: every n-bit value represents some subset. even better, if A is a subset of B then the number representing a is less than that representing B, which is convenient for some dynamic programming solutions.
It is also possible to iterate over all the subsets of a particle subset (represented by a bit pattern), provided that you don't mind visiting them in reverse order (if this is problematic, put them in a list as they're generated, then walk the list backwards ). the trick is similar to that for finding the lowest Bit in a number. if we subtract 1 from a subset, then the lowest set element is cleared, and every lower element is set. however, we only want to set those lower elements that are in the superset. so the iteration step is justI = (I-1) & superset.
Even a bit wrong scores zero
There are a few mistakes that are very easy to make when ming bit manipulations. Watch out for them in your code.
- When executing shift instructionsA <B, The X86 architecture uses only the bottom 5 bitsB(6 for 64-bit integers ). this means that shifting left (or right) by 32 does nothing, rather than clearing all the bits. this behaviour is also specified by the Java and C # language standards; c99 says that shifting by at least the size of the value gives an undefined result. historical trivia: The 8086 used the full shift register, and the change in behaviour was often used to detect newer processors.
- The&And|Operators have lower precedence than comparison operators. That means thatX & 3 = 1Is interpretedX & (3 = 1), Which is probably not what you want.
- If you want to write completely portable C/C ++ Code, be sure to use unsigned types, and if you plan to use the top-most bit. c99 says that shift operations on negative values are undefined. java only has signed types:>Will sign-extend values (which is probablyNotWhat you want), but the Java-specific operator>>>Will shift in zeros.
Cute tricks
There are a few other tricks that can be done with bit manipulation. They're good for amazing your friends, but generally not worth the effect to use in practice.
-
Reversing the bits in an integer
-
x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
As an exercise, see if you can adapt this to count the number of bits in a word.
-
Iterate through all K-element subsets of {0, 1 ,... N-1}
-
int s = (1 << k) - 1;
while (!(s & 1 << N))
{
// do stuff with s
int lo = s & ~(s - 1); // lowest one bit
int lz = (s + lo) & ~s; // lowest zero bit above lo
s |= lz; // add lz to the set
s &= ~(lz - 1); // reset bits below lz
s |= (lz / lo / 2) - 1; // put back right number of bits at end
}
In C, the last line can be writtenS | = (LZ> FFs (LO)-1To avoid the division.
-
Evaluate X? Y:-y, Where XIs 0 or 1
-
(-x ^ y) + x
This works on a twos-complement architecture (which is almost any machine you find today), where negation is done by inverting all the bits then adding 1. note that on i686 and above, the original expression can be evaluated just as efficiently (I. E ., without branches) due toCmove(Conditional move) instruction.
Sample Problems
TCCC 2006, round 1B Medium
For each city, keep a bit-set of the neighbouring cities. once the part-building factories have been chosen (recursively), Anding together these bit-sets will give a bit-set which describes the possible locations of the Part-assembly factories. if this bit-set has K bits, then there are KCM ways to allocate the part-assembly factories.
TCO 2006, Round 1 easy
The small number of nodes stronugly suggests that this is done by considering all possible subsets. for every possible subset we consider two possibilities: either the smallest-numbered node does not communicate at all, in which case we refer back to the subset that excludes it, or it communicates with some node, in which case we refer back to the subset that excludes both of these nodes. the resulting code is extremely short:
static int dp[1 << 18];
int SeparateConnections::howMany(vector <string> mat)
{
int N = mat.size();
int N2 = 1 << N;
dp[0] = 0;
for (int i = 1; i < N2; i++)
{
int bot = i & ~(i - 1);
int use = __builtin_ctz(bot);
dp[i] = dp[i ^ bot];
for (int j = use + 1; j < N; j++)
if ((i & (1 << j)) && mat[use][j] == 'Y')
dp[i] = max(dp[i], dp[i ^ bot ^ (1 << j)] + 2);
}
return dp[N2 - 1];
}
SRM 308, Division 1 medium
The Board contains 36 squares and the draughts are indistinguishable, so the possible positions can be encoded into 64-bit integers. the first step is to enumerate all the legal moves. any legal move can be encoded using three bit-fields:BeforeState,AfterState andMask, Which defines which parts of the before State are significant. The move can be made from the current State if(Current & Mask) = before; If it is made, the new State is(Current &~ Mask) | after.
SRM 320, Division 1 hard
The constraints tell us that there are at most 8 columns (if there are more, we can swap rows and columns), so it is feasible to consider every possible way to lay out a row. once we have this information, we can solve the remainder of the problem (refer to the match editorial for details ). we thus need a list of all N-bit integers which do not have two adjacent 1 bits, and we also need to know how many 1 bits there are in each such row. here is my code for this:
for (int i = 0; i < (1 << n); i++)
{
if (i & (i << 1)) continue;
pg.push_back(i);
pgb.push_back(__builtin_popcount(i));
}
By bmerry