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


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.

forums_mpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>    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
forums_mpAuthor Commented:

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
itsmeandnobodyelseCommented:
>>>> 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
Infinity08Commented:
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) */

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
Note that in my code, word_type is an unsigned integer type, not a bitset.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.