424,946 Members | 697 Online
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

# BITWISE SHIFT QUESTION?

 P: n/a ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another variable. here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 or long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 to extract bit field I should shift right then use a bit mask containing all ones. This will store the first 4 bits into bits1to3 right? I beleive this is correct! but anyone else have suggestions? Apr 13 '06 #1
4 Replies

 P: n/a "HARDCORECODER" writes: ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another variable. myvar & 0xf will do that, given your apparent definition of "first". here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 or long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 to extract bit field I should shift right then use a bit mask containing all ones. This will store the first 4 bits into bits1to3 right? I don't see why you want to do a shift if you want the least-significant 4 bits. -- "Welcome to the wonderful world of undefined behavior, where the demons are nasal and the DeathStation users are nervous." --Daniel Fox Apr 13 '06 #2

 P: n/a HARDCORECODER wrote: ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another variable. The "first 4 bits" is an ambiguous phrase, as is "bit 0, 1, 2 and 3". From which end do you wish to number them? here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 Your first statement, variable name and comment all disagree on what you're trying to do. As far as the statement goes you apparently want bits 1, 2, 3 and 4 (numbering the least significant bit 0) in positions 0, 1, 2 and 3. If you instead want the four least significant bits, you can simply do x & 0x0f. or long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 Similarly, you are extracting bits 12-15 here. If 'x' is a 16-bit unsigned integer, those are the four most significant bits. to extract bit field I should shift right then use a bit mask containing all ones. This will store the first 4 bits into bits1to3 right? Not quite. You may be off by one. The four least significant bits are 0-3, not 1-4, and they do not need to be shifted. S. Apr 14 '06 #3

 P: n/a Ben Pfaff wrote: "HARDCORECODER" writes: ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another variable. myvar & 0xf will do that, given your apparent definition of "first". here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 That "works" is you have 5-bit ints. or long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 And 16-bit longs. to extract bit field I should shift right then use a bit mask containing all ones. This will store the first 4 bits into bits1to3 right? I don't see why you want to do a shift if you want the least-significant 4 bits. I don't think ke doesn't want the least-significant bits -- it sounds more like he wants the most-significant bits. (At least, that's what it appears he wants. Maybe. Perhaps.) Or, maybe he's not sure what he wants. How about something like: ( foo >> ( (sizeof(foo) * CHAR_BITS) - 4 ) ) & 0x0f Am I correct that "sizeof(foo)*CHAR_BITS" gives you the number of bits in "foo"? -- +-------------------------+--------------------+-----------------------------+ | Kenneth J. Brody | www.hvcomputer.com | | | kenbrody/at\spamcop.net | www.fptech.com | #include | +-------------------------+--------------------+-----------------------------+ Don't e-mail me at: Apr 14 '06 #4

 P: n/a Kenneth Brody wrote On 04/14/06 12:58,: Ben Pfaff wrote:"HARDCORECODER" writes:ok I want to extract the first 4 bits of a 32 bit interger. The SIZEdoesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit0,1,2 and,3. I want to exract these and place into another variable.myvar & 0xf will do that, given your apparent definition of "first".here is my Ideaint bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 That "works" is you have 5-bit ints.orlong bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 And 16-bit longs.to extract bit field I should shift right then use a bit maskcontaining all ones. This will store the first 4 bits into bits1to3right?I don't see why you want to do a shift if you want theleast-significant 4 bits. I don't think ke doesn't want the least-significant bits -- it sounds more like he wants the most-significant bits. (At least, that's what it appears he wants. Maybe. Perhaps.) Or, maybe he's not sure what he wants. How about something like: ( foo >> ( (sizeof(foo) * CHAR_BITS) - 4 ) ) & 0x0f Am I correct that "sizeof(foo)*CHAR_BITS" gives you the number of bits in "foo"? It does, but there are two nagging difficulties. - If `foo' has padding bits, bits that are part of its representation but not part of its value, you'll be shifting by too many places. This seems to be mostly a "theoretical" concern, as C implementations where integer types have padding bits are about as rare as snowballs in Hell. Still, the Standard permits the peculiar practice, and if such a machine is ever built Murphy's Law ensures that your code will try to run on it. - If `foo' is a signed type with a negative value, the effect of right-shifting is undefined. It may or may not be possible to shift the sign bit into a value position and then isolate it with an AND; the results simply aren't predictable. This is one of the reasons why scarred veterans (where do you think the scars came from?) recommend using unsigned types for bit-walloping. -- Er*********@sun.com Apr 14 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.