Solved

# using std::bitset to set values within a range of bits

Posted on 2008-11-15
Medium Priority
1,057 Views

I'd like to set bits within a bit range given a certain value:  For instance
Given a value 8hex, set the appropriate bits in the range  0..3.
Given a value  6hex, set the appropriate bits in the range 4..6

For error checking I'd like to compare the given value against the min/max value within the range.
I'm struggling to do this with std::bitset.  Source code appreciated.

0
Question by:forums_mp
• 3

LVL 53

Expert Comment

ID: 22970081
>>    Given a value 8hex, set the appropriate bits in the range  0..3.
>>    Given a value  6hex, set the appropriate bits in the range 4..6

I don't understand what you're trying to do.

Do you mean you want to get this value :

1101000

?

Can you explain a bit more of the context ?
0

Author Comment

ID: 22971202

Given a  - lets say a 16 bit word.  Now given a function

typedef unsigned short word_type ;
word_type to_set ;
void set_bits ( int value, int start_bit, int stop_bit ) {

}
int get_value () { return to_set; }

Now if I give you a value 8hex with instructions to set the bits in range 0..3 in to_set.  When I call get_value I ought to get see 8hex.

Now if I give you a value 6hex with instructions to set the bits in the range 4..6 in to_set.  When I call get_value I should get the what you allude to in your post

Long story, short I'm being told to set bit ranges across - say a 16bit word.   So if I set bit 1, when I call get_value the value ought to be 1.  If i'm told to set bit 9, when I call get_value the value ought to be 257.   You get the point

I know std::bitset has some cool methods to work with bits so I thought maybe I ought to use that.

0

LVL 39

Expert Comment

ID: 22971293
>>>> I give you a value 8hex
hex and decimal are simply a kind of representation, Same as 'binary' in 1101000. It simply means to *print* an integer to a given base which either is 16, 10, or 2.

>>>> with instructions to set the bits in range 0..3 in to_set.
That actually is a different thing. The bit range 0 ... 3 allows to have integers from 0 to 15 decimal. So, the terminus '8hex' doesn't actuall fit to a range of 0...3.

If you want to set bits for a range of bits only you need to do what is called 'masking'. You first make the target bits clean by putting zero bits to them, e. g. by

value & ~(1<<i);

(the 1<<i sets the bit at i and the ~ makes all bits 1 but the one at i, finally the & clears the bit at i).

where i is the bit to clear. For a range of bits you would do that for each bit of the range.

for (i = start_bit; i <= stop_bit; ++i)
value & ~(1<<i);

You would set the bits given accordingly.

>>>> I know std::bitset has some cool methods to work with bits so
The main thing of std::bitset is that it could handle more than 32 bits. If you only want to deal with integers (forget the shorts!) you don't need bitset.

0

LVL 53

Accepted Solution

Infinity08 earned 200 total points
ID: 22971348
Is this what you mean ?
``````word_type to_set = 0;

void set_bits(int value, int start_bit, int stop_bit) {
word_type mask = (1 << (stop_bit - start_bit + 1)) - 1;
to_set &= ~(mask << start_bit);        /* erase old target bits */
to_set |= (value & mask) << start_bit; /* set new bits */
}

set_bits(0x08, 0, 3);  /* set_bits is now 0x08 (00001000) */
set_bits(0x06, 4, 6);  /* set_bits is now 0x68 (01101000) */
``````
0

LVL 53

Expert Comment

ID: 22971350
Note that in my code, word_type is an unsigned integer type, not a bitset.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month8 days, 16 hours left to enroll

#### 621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.