04 Jo P Apr 07


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

04 Jo P Apr 07

  1. 1. The Joy ofProgrammingHow ‘C’mart Are You?Some Bit-manipulation S.G. GANESHTricks and TechniquesIn this column, lets see few interesting bit-manipulation tips; such techniques are useful andwidely used in advanced low-level programming tasks such as writing software emulatorsand device drivers.It is assumed that the underlying implementation follows EXORing i with all 1s results in resetting the bits2’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 leftQ1: What does the expression “(i & (i – 1))” do? side and the expression is: (~i + 1 = -i); now you canA: 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 COLUMNQ2: 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 differenceQ3: 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 theA: (i ^ (~0)) and –(i + 1). two. If j is less than i, the expression addsQ4: What does the expression (j + ((i - j) & -(i < j)) do? the difference between the two; or else itA: 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 eitherA: ((i & j) + (i | j)) i or j. (i & j) shows the value that’s set inQ6: 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, theA: 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 asThat’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 www.linuxforu.com | LINUX FOR YOU | APRIL 2007 73 CMYK