-
Booleans in C
I need to find a way to do a C++ Booleans in C, the way I've come up with so far is:
Code:
typedef enum { false, true } bool;
bool one = false;
Which works fine. But, I need to know if this takes up less memory, than a char. Could I possibly take up one bit like 'int bool:1;' in a struct? Is there another less memory usage way?
Also, I thought I mention, I know there's a bool type in C99. But, I'm developing this for an embedded cpu, which I need to use a special compiler, libraries, the whole bit. I'm trying to save space.
-
>> But, I need to know if this takes up less memory, than a char
no, and it may even take up more space than that. I'm not sure if the standard dictates the size of an enum, but my guess is that it will range from 16-32 bits. personally, I don't think the size matters much for most purposes, though.
[edit]
ok, well in that case, why not use, say, a n-bit int to keep track of n separate boolean values? also, a structure like this should cast properly back and forth (between integral and struct representations):
Code:
#pragma push(pack, 1)
typedef struct eightbit {
unsigned a : 1;
unsigned b : 1;
unsigned c : 1;
unsigned d : 1;
unsigned e : 1;
unsigned f : 1;
unsigned g : 1;
unsigned h : 1;
};
#pragma push(pop)
#ifndef false
#define false 0
#define true !false
#endif
[/edit]
-
No data type is smaller than a char in C. Otherwise, what would sizeof return, zero? :D
Quzah.
-
> But, I need to know if this takes up less memory, than a char
IIRC, enums in C are either int or unsigned int depending on the range of values in the enumeration.
> Could I possibly take up one bit like 'int bool:1;' in a struct? Is there another less memory usage way?
Possibly, if you had a lot of bools to store.
Bear in mind that the more you compress the information, the more likely the compiler is to use extra instructions to store and retrieve the information.
Also remember that padding and alignment plays a part as well.
bool bVar;
int iVar;
If bool is a char, then there could be padding bytes which are completely unused between the bool and the int. Making bool an int in this case would be a free lunch.
Unless you have a specific problem in mind - like a large array of bools, I'd go with something obvious to start with. Premature optimisation really is the dark side.
-
To be honest, this question depends totally on your compiler.
I agree with Salem in that you should make your boolean the size of an int. This will make it the same size as your processer (8 bit, 16 bit, etc). Then you dont have to worry about padding or alignment :)
-
>I need to know if this takes up less memory, than a char.
It doesn't. Don't expect it to. Ever. The smallest addressable unit is a char, so the very best you can do is:
Code:
unsigned char one = 0;
>Could I possibly take up one bit like 'int bool:1;' in a struct?
No, this still takes up the space of an int, and you also have the interesting problem of the signed int for a one bit bitfield. Better to make it unsigned so that you know 0 and 1 are available rather than 0 and -1. You save yourself some head scratching. :)
>I'm trying to save space.
I'm sure there are better places to save space. Why not start with those?
-
Like Salem said it's very likely that the compiler will have to generate extra instructions to process the data. The bitfields may not be int-sized for your compiler but they are at least char-sized themselves. Oh and instructions are probably char-sized or bigger so just using chars would probably be both faster and smaller. ;)
Just out of curiosity, how much space do you have to work with? Because the smallest amount of space I've ever had to work with is 32k (Lego Mindstorms RCX).