Quick question...

• 05-28-2007
Queatrix
Quick question...
Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.
• 05-28-2007
ChaosEngine
you can have a single bit in a struct or class as part of a bitfield
Code:

```class bits {     unsigned int b1 : 1;     unsigned int b7 : 7; };```
note that using a single bit is actually much slower on most machines. why do you want this?
• 05-28-2007
Dave_Sinkula
Quote:

Originally Posted by Queatrix
Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.

• 05-28-2007
whiteflags
The smallest addressable piece of memory is a byte. You aren't going to work around it incredibly easily: you could do something like what Chaos Engine showed you, but the other bits are going to be rationed off anyway whether they're used or not used.
• 05-29-2007
Queatrix
The reason I was asking, is because I wanted to load a file into an array, that way, I can handle each binary bit with ease. Would Engine's solution work for this?
• 05-29-2007
Salem
> Would Engine's solution work for this?
Nope.
There is no way to determine whether unsigned int b1 : 1; is the MSB or LSB of a char, short or long.

Bit fields are an internal representation only. You can't use them to access external devices, file formats, message streams etc in any meaningful way.
• 05-29-2007
maxorator
Read a byte and then cycle through its bits with bitwise operators.
• 05-29-2007
robatino
There's vector<bool> (specialized to use just 1 bit, not byte, per element) and bitset<N> (where N is known at compile time).

• 05-29-2007
Queatrix
>> Read a byte and then cycle through its bits with bitwise operators.
That is what I am currently doing, I was just hoping there was a better way.

However, thanks for the responces all.
• 05-29-2007
Wraithan
Beware of vector<bool> it is not a normal/proper STL container, the algorithms that work on vectors are not guaranteed to work properly on vector<bool> since it is a specialized template. I ran into this problem before. Google a bit and become aware of the container you are using, since it isn't a traditional vector.
• 05-29-2007
Dave_Sinkula
Quote:

Originally Posted by Queatrix
>> Read a byte and then cycle through its bits with bitwise operators.
That is what I am currently doing, I was just hoping there was a better way.

This is the same thing, only different:
http://c-faq.com/misc/bitsets.html
• 05-30-2007
zacs7
So you wanted to read in from the file 'bit by bit'?

eh...
• 05-30-2007
Queatrix
@Zacs: Yup.