How to copy values from bitset to byte buffer?

A

Allen

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
 
M

Michael DOUBEZ

Allen said:
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.
 
B

Bertrand

Michael said:
Allen said:
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()
 
M

Michael DOUBEZ

Bertrand said:
Michael said:
Allen said:
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.
 
P

Pascal J. Bourguignon

Allen said:
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/
 
K

Kai-Uwe Bux

Pascal said:
Allen said:
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
 
P

Pascal J. Bourguignon

Kai-Uwe Bux said:
Pascal said:
Allen said:
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!
 
M

Michael DOUBEZ

Pascal said:
Kai-Uwe Bux said:
Pascal said:
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 said:
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.
 
B

Bertrand

Michael said:
Bertrand said:
Michael said:
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* ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top