|
-
Jun 6th, 2002, 03:23 AM
#1
Thread Starter
Lively Member
I don't want to waste memory
Hey everybody! I've got a question: You see, I only want to use 2 bits for a program. The first thing I did was create a 2-itemed bool array, but I couldn't print it out on the screen properly. So does anyone know how to just use a specific number of bits without having to waste the rest? Help would be greatly appreciated. Thanks!
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Jun 6th, 2002, 05:11 AM
#2
Frenzied Member
You can't only use 2 bits. The memory isn't addressed that way. The smallest variable you can use is a byte.
Harry.
"From one thing, know ten thousand things."
-
Jun 6th, 2002, 06:01 AM
#3
Thread Starter
Lively Member
Well, what about the bool type? That only uses one bit.
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
Jun 6th, 2002, 06:52 AM
#4
Frenzied Member
It doesn't. Boolean variables are just typedefs for an integer type, and I have a feeling that's a 32 bit integer. zero is false and anything non-zero is true.
You can pack 32 bit flags into a 32-bit int if you want, but you can only access/modify those flags using bit-masking. The memory cannot be addressed in bits.
Harry.
"From one thing, know ten thousand things."
-
Jun 6th, 2002, 08:21 AM
#5
You can use a bitset container for bit flags, but it still allocates memory in blocks of 32 bits for faster access.
And to be serious: there isn't any difference between using 2 bits or 32 bits if you don't do it 2000 times. This is not the '60s. If you do it 2000 times - well then you don't need 2 bits, but 4000 bits .
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 6th, 2002, 09:51 PM
#6
Hyperactive Member
Bool is an enum type, I heard.
-
Jun 7th, 2002, 02:47 AM
#7
bool types:
BOOL is a typdef of windows = int. TRUE is a define for 1, FALSE is 0.
bool is a native C++ datatype. Size depends on the compiler, usually 32 bit. But a 64-bit gcc version first had a 64-bit bool, then changed it to 8 bit in a later release.
VARIANT_BOOL is again a typedef of windows to int. In that case, true is -1 (0xFFFFFFFF) and false is 0, as in VB.
I don't know about others.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 11th, 2002, 08:10 AM
#8
Frenzied Member
Originally posted by transcendental
Bool is an enum type, I heard.
Enums are still integers behind the scenes. You have to store the value somewhere, and in most cases that's in a 32-bit variable.
Harry.
"From one thing, know ten thousand things."
-
Jun 12th, 2002, 11:56 AM
#9
Monday Morning Lunatic
To extend, make no assumptions about the size of anything, except char (which I think is guaranteed to be 1, whether or not it actually takes up 1 byte or not )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 12th, 2002, 03:42 PM
#10
Yep, char is guaranteed to be 1 minimum unit, 8 bits (1 byte) on a pc, but not necessarily on all computers.
At least I think so.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 12th, 2002, 03:45 PM
#11
Monday Morning Lunatic
I'm not sure of implementation details (such as on systems where you don't have bits and bytes, or if a byte is 10 bits....... ) but I think sizeof(char) is defined in the standard to be "1".
Anyone got a copy of the standard? I *really* ought to buy myself one at some point
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 12th, 2002, 04:16 PM
#12
Yeah, if it wasn't that you have to pay I would have gotten the standard long ago.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 12th, 2002, 04:21 PM
#13
Monday Morning Lunatic
If it would actually do any good, I'd have got a copy of the standard through work (for work purposes) on expenses, but somehow I doubt anyone round here is actually bothered about following standards and are quite happy to churn out incorrect code...........*sigh*
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|