(E-Mail Removed) wrote:

>

> pete wrote:

> > (E-Mail Removed) wrote:

> > >

> > > Digital Puer wrote:

> > > > Hi, suppose I have an unsigned long long.

> > > > I would like to extract

> > > > the front 'n' bits of this value and convert them

> > > > into an integer.

> > > > For example, if I extract the first 3 bits,

> > > > I would get an int between

> > > > 0 and 7 (=2^3-1). Could someone please help out?

> >

> > > I'm guessing long long is 64 bits?

> > > Regardless, the following should

> > > work:

> >

> > > frontbits =

> > > (original_value >> (sizeof(long long) - 3)) & 0x03;

> >

> > You're getting 2 bits from the middle of the value.

> > Anything & 3, ain't going to get you more than 2 bits.

> > You shift the original value to the right

> > and the low order bits disappear.

> Ah, sorry, it should be:

>

> frontbits = (original_value >> (sizeof(long long) - 3)) & 0x07;
Barry Schwarz also seems to think that shifting the original

value to the right, is the right thing to do.

I don't get it.

I'll assume you think that CHAR_BIT is 8.

That makes your 64 bit bit long long, 8 bytes in size.

8 - 3 is two, so you shift your unsigned long long

two bits to the right, which is equivalent to dividing by 4,

and then you take the 3 lower order bits.

What is that?

--

pete