Is there a 1 bit varible type? And I'm not talking 1 byte (8 bit), like char.
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; };
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Read a byte and then cycle through its bits with bitwise operators.
"The Internet treats censorship as damage and routes around it." - John Gilmore
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
Last edited by robatino; 05-29-2007 at 09:04 AM.
>> 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
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
Last edited by Queatrix; 05-30-2007 at 06:02 AM.