How to copy values from bitset to byte buffer?

Discussion in 'C++' started by Allen, Jan 22, 2009.

  1. Allen

    Allen Guest

    I use bitset to save bits of flag. When data is ready, I need to copy
    bit values from it to byte buffer. How to do it?

    Example.

    #include <bitset>
    int main()
    {
    char buffer[32];
    std::bitset<256> fcbits;
    fcbits.set(1);
    fcbits.set(54);
    // fcbits -> buffer?
    }

    Allen
     
    Allen, Jan 22, 2009
    #1
    1. Advertising

  2. Allen wrote:
    > I use bitset to save bits of flag. When data is ready, I need to copy
    > bit values from it to byte buffer. How to do it?


    By hand.

    > Example.
    >
    > #include <bitset>
    > int main()
    > {
    > char buffer[32];
    > std::bitset<256> fcbits;
    > fcbits.set(1);
    > fcbits.set(54);
    > // fcbits -> buffer?
    > }


    I assume bit n goes to buffer[n/8],bit(n%8).
    for(int i=0;i<fcbits.size();++i)
    {
    const int index=n/8;
    const char mask=1<<(n%8);
    if(fcbits)buffer[index]|=mask;
    else buffer[index]&=~mask;
    }

    There may be more optimal ways to do it.

    --
    Michael
     
    Michael DOUBEZ, Jan 22, 2009
    #2
    1. Advertising

  3. Allen

    Bertrand Guest

    Michael DOUBEZ wrote:
    > Allen wrote:
    >> I use bitset to save bits of flag. When data is ready, I need to copy
    >> bit values from it to byte buffer. How to do it?

    >
    > By hand.
    >
    >> Example.
    >>
    >> #include <bitset>
    >> int main()
    >> {
    >> char buffer[32];
    >> std::bitset<256> fcbits;
    >> fcbits.set(1);
    >> fcbits.set(54);
    >> // fcbits -> buffer?
    >> }

    >
    > I assume bit n goes to buffer[n/8],bit(n%8).
    > for(int i=0;i<fcbits.size();++i)
    > {
    > const int index=n/8;
    > const char mask=1<<(n%8);
    > if(fcbits)buffer[index]|=mask;
    > else buffer[index]&=~mask;
    > }
    >
    > There may be more optimal ways to do it.
    >

    if the size allows it, there is also
    fcbits.to_ulong()

    --
    Bertrand
     
    Bertrand, Jan 22, 2009
    #3
  4. Bertrand wrote:
    > Michael DOUBEZ wrote:
    >> Allen wrote:
    >>> I use bitset to save bits of flag. When data is ready, I need to copy
    >>> bit values from it to byte buffer. How to do it?

    [snip]
    >>> std::bitset<256> fcbits;

    [snip]
    > if the size allows it, there is also
    > fcbits.to_ulong()


    With 256 bits, I don't think it will work.

    --
    Michael
     
    Michael DOUBEZ, Jan 23, 2009
    #4
  5. how about reinterpret_cast ?

    On Jan 22, 9:05 am, Allen <> wrote:
    > I use bitset to save bits of flag. When data is ready, I need to copy
    > bit values from it to byte buffer. How to do it?
    >
    > Example.
    >
    > #include <bitset>
    > int main()
    > {
    >   char buffer[32];
    >   std::bitset<256> fcbits;
    >   fcbits.set(1);
    >   fcbits.set(54);
    >   // fcbits -> buffer?
    >
    > }
    >
    > Allen
     
    Hanos-Puskai Péter, Jan 23, 2009
    #5
  6. Allen <> writes:

    > I use bitset to save bits of flag. When data is ready, I need to copy
    > bit values from it to byte buffer. How to do it?


    Yes, how? How do you want to represent your bits as a string of characters?


    > Example.
    >
    > #include <bitset>
    > int main()
    > {
    > char buffer[32];
    > std::bitset<256> fcbits;
    > fcbits.set(1);
    > fcbits.set(54);
    > // fcbits -> buffer?
    > }



    There's not enough bytes in your buffer.

    Surprized? That may be because you don't give a specification precise enough.

    Otherwise, why don't you read the reference to the bitset class?
    http://www.cplusplus.com/reference/stl/bitset/

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Jan 23, 2009
    #6
  7. Allen

    Kai-Uwe Bux Guest

    Pascal J. Bourguignon wrote:

    > Allen <> writes:
    >
    >> I use bitset to save bits of flag. When data is ready, I need to copy
    >> bit values from it to byte buffer. How to do it?

    >
    > Yes, how? How do you want to represent your bits as a string of
    > characters?
    >
    >
    >> Example.
    >>
    >> #include <bitset>
    >> int main()
    >> {
    >> char buffer[32];
    >> std::bitset<256> fcbits;
    >> fcbits.set(1);
    >> fcbits.set(54);
    >> // fcbits -> buffer?
    >> }

    >
    >
    > There's not enough bytes in your buffer.


    That sounds very definite. How did you tell?

    > Surprized? That may be because you don't give a specification precise
    > enough.


    In that case, I think, all one could say is that for _some_ specifications,
    there are not enough bytes in the buffer. For _some_ other specifications,
    there are.

    > Otherwise, why don't you read the reference to the bitset class?
    > http://www.cplusplus.com/reference/stl/bitset/


    Or the standard. Now, what particularly is it that the OP should pay
    attention to? After all, RTFM is most often not a good answer in a news
    group where everything that couldn't be answered that way is off-topic :)


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 23, 2009
    #7
  8. Kai-Uwe Bux <> writes:

    > Pascal J. Bourguignon wrote:
    >
    >> Allen <> writes:
    >>
    >>> I use bitset to save bits of flag. When data is ready, I need to copy
    >>> bit values from it to byte buffer. How to do it?

    >>
    >> Yes, how? How do you want to represent your bits as a string of
    >> characters?
    >>
    >>
    >>> Example.
    >>>
    >>> #include <bitset>
    >>> int main()
    >>> {
    >>> char buffer[32];
    >>> std::bitset<256> fcbits;
    >>> fcbits.set(1);
    >>> fcbits.set(54);
    >>> // fcbits -> buffer?
    >>> }

    >>
    >>
    >> There's not enough bytes in your buffer.

    >
    > That sounds very definite. How did you tell?
    >
    >> Surprized? That may be because you don't give a specification precise
    >> enough.

    >
    > In that case, I think, all one could say is that for _some_ specifications,
    > there are not enough bytes in the buffer. For _some_ other specifications,
    > there are.
    >
    >> Otherwise, why don't you read the reference to the bitset class?
    >> http://www.cplusplus.com/reference/stl/bitset/

    >
    > Or the standard. Now, what particularly is it that the OP should pay
    > attention to? After all, RTFM is most often not a good answer in a news
    > group where everything that couldn't be answered that way is off-topic :)


    I would use fcbits.to_string().
    In C++, we don't use char[] a lot, we rather use std::string.
    bitset::to_string() copies the bit values to a character buffer.
    If something else is needed, it must be specified!

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Jan 23, 2009
    #8
  9. Pascal J. Bourguignon wrote:
    > Kai-Uwe Bux <> writes:
    >
    >> Pascal J. Bourguignon wrote:
    >>
    >>> Allen <> writes:
    >>>
    >>>> I use bitset to save bits of flag. When data is ready, I need to copy
    >>>> bit values from it to byte buffer. How to do it?
    >>> Yes, how? How do you want to represent your bits as a string of
    >>> characters?
    >>>
    >>>
    >>>> Example.
    >>>>
    >>>> #include <bitset>
    >>>> int main()
    >>>> {
    >>>> char buffer[32];
    >>>> std::bitset<256> fcbits;
    >>>> fcbits.set(1);
    >>>> fcbits.set(54);
    >>>> // fcbits -> buffer?
    >>>> }
    >>>
    >>> There's not enough bytes in your buffer.

    >> That sounds very definite. How did you tell?
    >>
    >>> Surprized? That may be because you don't give a specification precise
    >>> enough.

    >> In that case, I think, all one could say is that for _some_ specifications,
    >> there are not enough bytes in the buffer. For _some_ other specifications,
    >> there are.
    >>
    >>> Otherwise, why don't you read the reference to the bitset class?
    >>> http://www.cplusplus.com/reference/stl/bitset/

    >> Or the standard. Now, what particularly is it that the OP should pay
    >> attention to? After all, RTFM is most often not a good answer in a news
    >> group where everything that couldn't be answered that way is off-topic :)

    >
    > I would use fcbits.to_string().


    In fact you would use to_string<char,char_traits<char>,allocator<char> >().

    Which gives you a string of 256 element which is clearly not what the OP
    wants since he wants to copy bit values in bit buffer.

    > In C++, we don't use char[] a lot, we rather use std::string.


    I still use char[] and vector<char> also.

    > bitset::to_string() copies the bit values to a character buffer.


    It construct a string with a serie of 0 and 1 that represent the values
    of the bits which is not the same.

    --
    Michael
     
    Michael DOUBEZ, Jan 23, 2009
    #9
  10. Allen

    Bertrand Guest

    Michael DOUBEZ wrote:
    > Bertrand wrote:
    >> Michael DOUBEZ wrote:
    >>> Allen wrote:
    >>>> I use bitset to save bits of flag. When data is ready, I need to copy
    >>>> bit values from it to byte buffer. How to do it?

    > [snip]
    >>>> std::bitset<256> fcbits;

    > [snip]
    >> if the size allows it, there is also
    >> fcbits.to_ulong()

    >
    > With 256 bits, I don't think it will work.
    >

    That's why I wrote: *if the size allows it* ;-)

    --
    Bertrand
     
    Bertrand, Jan 26, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince
    Replies:
    16
    Views:
    5,621
    White Wolf
    Jan 20, 2005
  2. David Garamond

    Getting back a byte string from BitSet

    David Garamond, Feb 18, 2004, in forum: Ruby
    Replies:
    0
    Views:
    131
    David Garamond
    Feb 18, 2004
  3. Replies:
    1
    Views:
    312
  4. Mike Copeland

    bitset Multiple Values Assignment

    Mike Copeland, Jun 11, 2012, in forum: C++
    Replies:
    1
    Views:
    506
    Luca Risolia
    Jun 11, 2012
  5. Ninds
    Replies:
    14
    Views:
    749
    W Karas
    Dec 3, 2012
Loading...

Share This Page