Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.
Printable View
Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.
you can have a single bit in a struct or class as part of a bitfield
note that using a single bit is actually much slower on most machines. why do you want this?Code:class bits
{
unsigned int b1 : 1;
unsigned int b7 : 7;
};
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.
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?
> 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.
Read a byte and then cycle through its bits with bitwise operators.
There's vector<bool> (specialized to use just 1 bit, not byte, per element) and bitset<N> (where N is known at compile time).
Edit: http://www.thescripts.com/forum/thread60019.html
>> 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.
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.
This is the same thing, only different:
http://c-faq.com/misc/bitsets.html
So you wanted to read in from the file 'bit by bit'?
eh...
@Zacs: Yup.
@Dave: Like the link.
@Wraithan: Okay, I figured I'm not going to use it anyway.