![]() |
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); |
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 |
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. |
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 |
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 |
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>> |
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. |
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; |
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 |
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 03:42 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.