BITWISE SHIFT QUESTION?

H

HARDCORECODER

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?
 
B

Ben Pfaff

HARDCORECODER said:
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.
 
S

Skarmander

HARDCORECODER said:
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.
 
K

Kenneth Brody

Ben said:
myvar & 0xf will do that, given your apparent definition of "first".

That "works" is you have 5-bit ints.

And 16-bit longs.
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 <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Kenneth Brody wrote On 04/14/06 12:58,:
That "works" is you have 5-bit ints.




And 16-bit longs.




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

BITWISE SHIFT question 13
Bitwise right shift 5
bitwise shift help 5
Machine Learning.. Endless Struggle 3
6.5.7.4 Bitwise shift operators 2
More bitwise woes 7
using picc on a pic1f627 13
Bitwise question 13

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top