Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to copy values from bitset to byte buffer?

Reply
Thread Tools

How to copy values from bitset to byte buffer?

 
 
Allen
Guest
Posts: n/a
 
      01-22-2009
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
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-22-2009
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%.
for(int i=0;i<fcbits.size();++i)
{
const int index=n/8;
const char mask=1<<(n%;
if(fcbits[i])buffer[index]|=mask;
else buffer[index]&=~mask;
}

There may be more optimal ways to do it.

--
Michael
 
Reply With Quote
 
 
 
 
Bertrand
Guest
Posts: n/a
 
      01-22-2009
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%.
> for(int i=0;i<fcbits.size();++i)
> {
> const int index=n/8;
> const char mask=1<<(n%;
> if(fcbits[i])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
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-23-2009
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
 
Reply With Quote
 
Hanos-Puskai Péter
Guest
Posts: n/a
 
      01-23-2009
how about reinterpret_cast ?

On Jan 22, 9:05*am, Allen <Allen.Che...@gmail.com> 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

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      01-23-2009
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__
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-23-2009
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
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      01-23-2009
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__
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-23-2009
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
 
Reply With Quote
 
Bertrand
Guest
Posts: n/a
 
      01-26-2009
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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
bitset<32> and bitset<64> efficiency Ninds C++ 14 12-03-2012 11:02 PM
bitset Multiple Values Assignment Mike Copeland C++ 1 06-11-2012 02:27 AM
copy content of vector<BYTE> to BYTE* Vince C++ 16 01-20-2005 10:32 PM
Getting back a byte string from BitSet David Garamond Ruby 0 02-18-2004 04:05 PM



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