Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   extracting front bits from an unsigned long long? (http://www.velocityreviews.com/forums/t440106-extracting-front-bits-from-an-unsigned-long-long.html)

Digital Puer 11-11-2005 12:59 AM

extracting front bits from an unsigned long long?
 
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 11-11-2005 01:22 AM

Re: extracting front bits from an unsigned long long?
 
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 11-11-2005 01:23 AM

Re: extracting front bits from an unsigned long long?
 
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 11-11-2005 02:03 AM

Re: extracting front bits from an unsigned long long?
 
slebetman@yahoo.com 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 11-11-2005 02:09 AM

Re: extracting front bits from an unsigned long long?
 
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 11-11-2005 02:11 AM

Re: extracting front bits from an unsigned long long?
 
On 10 Nov 2005 16:59:21 -0800, "Digital Puer"
<digital_puer@hotmail.com> 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 11-11-2005 02:30 AM

Re: extracting front bits from an unsigned long long?
 
"Digital Puer" <digital_puer@hotmail.com> 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) kst-u@mib.org <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 11-11-2005 02:31 AM

Re: extracting front bits from an unsigned long long?
 

pete wrote:
> slebetman@yahoo.com 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 11-11-2005 02:39 AM

Re: extracting front bits from an unsigned long long?
 
slebetman@yahoo.com wrote:
>
> pete wrote:
> > slebetman@yahoo.com 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 11-11-2005 02:43 AM

Re: extracting front bits from an unsigned long long?
 
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


All times are GMT. The time now is 08:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.