views:

309

answers:

2
+4  Q: 

Extracting bits

In C, I have a 32bit word representing an address (and I have it stored in a unsigned long, hope thats ok). Now from what I gather, part of an address contains the page number and the other part contains the offset. I was wondering how I could extract just the bits that give me the page number. I have already worked out the first 22 most significant bits are the page number and the other 10 bits are the page offset. How can I grab just the bits that are the page number? I am thinking I can do this with some bitwise operations, but am not sure how.

Thanks! Any help would be most appreciated

+8  A: 

Use the bitshift operators to extract the bits you need.

pageNumber = x >> 10;
offset = x & ((1 << 10) - 1);

For the page number, the >> operator shifts bits down, so you lose the least signifcant bits.

For the offset, ((1 << 10) - 1) creates a bitmask consisting of 10 ones which is used to select only the 10 least significant bits and ignore the most significant bits.

Mark Byers
It might be a good idea on code like this to mask the bits after you shifted them, in case your hardware does an arithmetic (sign-extending) right shift.pagenumber = (x >> 10)
John R. Strohm
+1  A: 

I'm a huge fan of the "two shifts" method of field extraction. It works both signed and unsigned. To extract a field of width w with least significant bit lsb from word:

#define BITSIN(W) (8*sizeof(W))
return (word << (BITSIN(word) - (lsb+width))) >> (BITSIN(word) - width);

In this case, BITSIN(word) == 32 and lsb+width == 32, so as long as the word in question is unsigned, you can just shift right 10 without masking.

One caution: beware 32-bit shifts on 32-bit types! The C standard lets the compiler do anything, and what the common Intel chips do is not useful: x << y shifts x left by y % 32 bits (provided x has a 32-bit integer type). This means if you try to shift a 32-bit integer left or right by 32 bits, the result is the same as a no-op. There is a similar issue with 64-bit shifts of 64-bit types.

Norman Ramsey
"what the common Intel chips do is not useful" - what do they do?
AShelly
@ASHelly: good question; I edited the answer. Who knows, it might get me an upvote :-)
Norman Ramsey