Velocity Reviews > extracting front bits from an unsigned long long?

# extracting front bits from an unsigned long long?

Digital Puer
Guest
Posts: n/a

 11-11-2005
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 can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.

Ideally, I'd like to implement a prototype like:
int extractFrontBits(unsigned long long value, int num_bits);

pete
Guest
Posts: n/a

 11-11-2005
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?

int three_bits = long_long_value & 7;

--
pete

slebetman@yahoo.com
Guest
Posts: n/a

 11-11-2005
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 can assume the largest returned value fits in an int. Also,
> I'm on a big-endian PPC (AIX), in case that matters.
>
> Ideally, I'd like to implement a prototype like:
> int extractFrontBits(unsigned long long value, int num_bits);

I'm guessing long long is 64 bits? Regardless, the following should
work:

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

The masking at the end is to zero out the msb of the result since on
some systems it doesn't get zeroed.

If number of bits is not fixed, then it would look something like the
following:

frontbits = original_value >> (sizeof(long long) - num_bits);

but the masking will get complicated.

pete
Guest
Posts: n/a

 11-11-2005
http://www.velocityreviews.com/forums/(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.

--
pete

pete
Guest
Posts: n/a

 11-11-2005
pete 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?

>
> int three_bits = long_long_value & 7;

n bits, ... hmm ...

long_long_value & ((1ULL << n) - 1)

--
pete

Barry Schwarz
Guest
Posts: n/a

 11-11-2005
On 10 Nov 2005 16:59:21 -0800, "Digital Puer"
<(E-Mail Removed)> 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 can assume the largest returned value fits in an int. Also,
>I'm on a big-endian PPC (AIX), in case that matters.
>
>Ideally, I'd like to implement a prototype like:
>int extractFrontBits(unsigned long long value, int num_bits);

Shift the value in the variable to the right
(sizeof(unsigned long long)*CHAR_BIT - n)
bits. The result is the integer you requested.

<<Remove the del for email>>

Keith Thompson
Guest
Posts: n/a

 11-11-2005
"Digital Puer" <(E-Mail Removed)> writes:
> 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?

What do you mean by "front" bits?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

slebetman@yahoo.com
Guest
Posts: n/a

 11-11-2005

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.
>
> --
> pete

Ah, sorry, it should be:

frontbits = (original_value >> (sizeof(long long) - 3)) & 0x07;

pete
Guest
Posts: n/a

 11-11-2005
(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

pete
Guest
Posts: n/a

 11-11-2005
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 can assume the largest returned value fits in an int. Also,
> I'm on a big-endian PPC (AIX), in case that matters.
>
> Ideally, I'd like to implement a prototype like:
> int extractFrontBits(unsigned long long value, int num_bits);

int extractFrontBits(unsigned long long value, int num_bits)
{
return (int)(value & ((1ULL << num_bits) - 1))
}

--
pete

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post krunalb C Programming 10 01-23-2007 02:47 PM sarmin kho Python 2 06-15-2004 06:40 PM Miki Tebeka Python 1 06-14-2004 03:19 PM Al Dykes Digital Photography 3 12-29-2003 07:08 PM George Marsaglia C Programming 1 07-08-2003 05:16 PM

Advertisments