Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > creating int from contiguous bits in an int

Reply
Thread Tools

creating int from contiguous bits in an int

 
 
Digital Puer
Guest
Posts: n/a
 
      11-26-2005
I'd like to be able to create an integer from a range of contiguous
bits
in another integer. I'm on a big-ending machine, in case that matters.
I would ideally like to create a function with this prototype:

unsigned long long extractValue(unsigned long long target, int a, int
b)

where a and b are the range of bits counted off from the most
significant
bit (where the msb is the 0th bit). For instance, 12345678900 is

00000000 00000000 00000000 00000010 11011111 11011100 00011100
00110100

A call to extractValue(value, 32, 35) should result in 13 (1101 in
binary).

Can someone please help?

 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      11-26-2005
Digital Puer wrote:
> I'd like to be able to create an integer from a range of contiguous
> bits
> in another integer. I'm on a big-ending machine, in case that matters.
> I would ideally like to create a function with this prototype:
>
> unsigned long long extractValue(unsigned long long target, int a, int
> b)
>
> where a and b are the range of bits counted off from the most
> significant
> bit (where the msb is the 0th bit). For instance, 12345678900 is
>
> 00000000 00000000 00000000 00000010 11011111 11011100 00011100
> 00110100
>
> A call to extractValue(value, 32, 35) should result in 13 (1101 in
> binary).
>
> Can someone please help?
>

You've chosen an unhelpful way of specifying the bits you want.

The idea is that we shift enough bits to the left for the desired MSB to
be the actual MSB, then shift back to make the desired LSB the actual
LSB, losing undesired bits in the overflow. This can be done in one
expression, but I'll do it in more for clarity.

#include <limits.h>

unsigned long long extractValue(unsigned long long target, int msb,
int lsb) {
const size_t ull_bits = CHAR_BIT * sizeof(unsigned long long);
unsigned long long result;
if (msb > lsb) {
/* empty range */
result = 0;
} else {
/* shift the desired msb to the actual msb */
result = target << msb;
/* there are (lsb - msb + 1) bits in the range, shift them
from high to low */
result >>= ull_bits - (lsb - msb + 1);
}
return result;
}

This function does not check if the arguments are in range; it assumes
you know what bits you can access.

S.
 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      11-26-2005
Digital Puer wrote:
> I'd like to be able to create an integer from a range of contiguous
> bits
> in another integer. I'm on a big-ending machine, in case that matters.
> I would ideally like to create a function with this prototype:
>
> unsigned long long extractValue(unsigned long long target, int a, int
> b)
>
> where a and b are the range of bits counted off from the most
> significant
> bit (where the msb is the 0th bit). For instance, 12345678900 is
>
> 00000000 00000000 00000000 00000010 11011111 11011100 00011100
> 00110100
>
> A call to extractValue(value, 32, 35) should result in 13 (1101 in
> binary).
>
> Can someone please help?
>

I'll try.
First though, why declare msb the 0th bit? It seems arbitrary and
counterintuitive in C where we think of the lsb as the 0th. Endianess
does not seem to be an issue.

Using 32 and 35 calculate that you need a four-bit mask and create it.

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001111

Now shift 'value' right such that the four bits of interest are the
least significant. Now 'and' the shifted value with the mask. Voila, 13.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      11-26-2005

Skarmander wrote:
> You've chosen an unhelpful way of specifying the bits you want.


What's a better way of specifying the bits?



> unsigned long long extractValue(unsigned long long target, int msb,
> int lsb) {



thank you. This works perfectly.

 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      11-26-2005

Joe Wright wrote:

> I'll try.
> First though, why declare msb the 0th bit? It seems arbitrary and
> counterintuitive in C where we think of the lsb as the 0th.


Sorry. I guess I'm thinking of the bit representation as a
character array, so '0010' would have the '1' bit in the
array[2] position. That's probably not too helpful.



>
> Using 32 and 35 calculate that you need a four-bit mask and create it.
>
> 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001111
>
> Now shift 'value' right such that the four bits of interest are the
> least significant. Now 'and' the shifted value with the mask. Voila, 13.
>



Thank you for the insight. Here's an implemention based on
your suggestion.


unsigned long long extract(unsigned long long target, int msb, int lsb)
{
int mask_size = lsb - msb + 1;
unsigned long long mask;
unsigned long long result;
int ull_size = CHAR_BIT * sizeof(unsigned long long);

mask = (1 << mask_size) - 1;
target >>= (ull_size - lsb - 1);
result = mask & target;

return result;

}

 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      11-27-2005
Digital Puer wrote:
> Skarmander wrote:
>
>>You've chosen an unhelpful way of specifying the bits you want.

>
>
> What's a better way of specifying the bits?
>

Well, bits are usually numbered from the LSB, starting at 0. Also, a
range specified by its ends needs checking to handle the case when it's
empty. An easier function to write would be

unsigned long long extractBits(unsigned long long source, int low,
int count) {
return (source >> low) && ((1ull << count) - 1);
}

Shorter, clearer, and it doesn't actually need to know how many bits are
in an unsigned long long. But, of course, this may not be what you need
at all, and may instead make things more difficult at the calling end. I
was just making the observation that *if* I had been free to write the
function as conveniently as possible, this would be its interface.

S.
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-27-2005
Digital Puer wrote:
> Skarmander wrote:
>
>>You've chosen an unhelpful way of specifying the bits you want.

>
> What's a better way of specifying the bits?


Specify them as you do with decimal digits
42 base 10
means
4 * 10^1 + 2 * 10^0

Just in the same way,
10 base 2
means
1 * 2^1 + 0 * 2^0

You can leave out leading digits evaluating to zero.
So, if talking about bit 0 (the binary digit associated
with 2^0), everyone knows that it is the least
significant bit and toggles +0/+1 -- irrespective of the
absolute number of available bits.
So, you'd rather go at it like that:
binary ... x ... 1 0 1 1
bit no ... n ... 3 2 1 0

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Gregory Pietsch
Guest
Posts: n/a
 
      11-28-2005

Digital Puer wrote:
> I'd like to be able to create an integer from a range of contiguous
> bits
> in another integer. I'm on a big-ending machine, in case that matters.
> I would ideally like to create a function with this prototype:
>
> unsigned long long extractValue(unsigned long long target, int a, int
> b)
>
> where a and b are the range of bits counted off from the most
> significant
> bit (where the msb is the 0th bit). For instance, 12345678900 is
>
> 00000000 00000000 00000000 00000010 11011111 11011100 00011100
> 00110100
>
> A call to extractValue(value, 32, 35) should result in 13 (1101 in
> binary).
>
> Can someone please help?


It's easier going from the lsb rather than the msb, because it makes
the shifting easier. Then you have:

unsigned long long
extractValue(unsigned long long target, int a, int b)
{
return (target >> a) & ~(~0ULL << (b - a + 1));
}

Rewriting this to work going from the msb is left as an exercise to the
reader.

Gregory Pietsch

 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
Count maximum contiguous set bits in an integer . Vish C Programming 3 04-29-2005 04:49 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57