Joy of Programming - Apr-07
Joy of Programming - Apr-07
Programming
How ‘C’mart Are You?
Some Bit-manipulation S.G. GANESH
It is assumed that the underlying implementation follows EXORing i with all 1s results in resetting the bits
2’s complement representation for integers. that are set; so it’s the same as ~i!
Assume that i, j and r are integers. In ~i = -(i + 1) implies ~i = (-i -1). Move -1 to the left
Q1: What does the expression “(i & (i – 1))” do? side and the expression is: (~i + 1 = -i); now you can
A: It checks if i is a power of 2 or not! read it as “1’s complement of i plus 1 is 2’s
GUEST COLUMN
Q2: What does the expression (i & (-i)) do? complement of i”, which is correct!
A: It returns the lowest bit set in an integer (which is a Q 4: (j + ((i - j) & -(i < j)) – let’s read it as: With
power of 2)! j (it can be i also), add the difference
Q3: How can you express ~i in terms of other (preferably between the two, and (re)set the sign bit
bit-wise) operators? for the value of the difference between the
A: (i ^ (~0)) and –(i + 1). two. If j is less than i, the expression adds
Q4: What does the expression (j + ((i - j) & -(i < j)) do? the difference between the two; or else it
A: This code results in the maximum of two integers i subtracts the difference between the two.
and j. Following the same logic (j - ((i - j) & -(i <
Q5: How can you express the expression (i + j) in terms j)) is the minimum of i and j.
of bit-wise ‘&’ and ‘|’ operations? Q 5: (i | j) sets the bits that are only set in either
A: ((i & j) + (i | j)) i or j. (i & j) shows the value that’s set in
Q6: What is the significance of the expression r = (i ^ j ^ both the values. Adding these two results
r)? has the effect of retaining the bits set, the
A: If r is equal to i, r will be reset to j; else if r is equal to bits set. Then adding the bits set in both the
j, r will be reset to i! values again with the result, if you closely
observe, is the same as, it is the same as
That’s enough; let’s see the explanation. (i + j).
Q1. When i is a value that’s a power of 2, only a single bit Q 6: If r is equal to i, it will nullify the effect of it
will be set in that integer. Now, when you subtract 1 in the expression (i ^ j ^ r) resulting in j;
from that value, that bit will be reset and will result the same happens if r is equal to j. (This is the same
in setting the lower bits. For (i & (i – 1)), when it is a trick used to re-write a doubly linked list—with
power of 2, the resulting two bit patterns will have previous and next pointers—with a single pointer,
no common bits set, so the expression becomes false which has the EXORed result of the previous and
(in that case it’s a power of 2); else it isn’t. next pointers!)
Q 2: -i is 1’s complement of i plus 1; when you invert all
the bits in an integer and add 1 to it, all the set bits S.G. Ganesh is an engineer in Hewlett-Packard’s C++
in the end are reset to zero. When we do ‘&’ of these compiler team. He has authored a book “Deep C” (ISBN 81-
two values, we get only the lowest bit set (since all 7656-501-6). He is also a member of the ANSI/ISO C++
other bits will have complementary values set in Standardisation committee (JTC1/SC22/WG21),
representing HP. You can reach him at
them).
sgganesh@gmail.com.
Q 3: ~i is 1’s complement of i; since ~0 is all 1’s set and
CMYK