Thread: Booleans in C

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    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.
    Last edited by KneeLess; 09-08-2004 at 06:59 PM.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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]
    Last edited by Sebastiani; 09-08-2004 at 07:15 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No data type is smaller than a char in C. Otherwise, what would sizeof return, zero?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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?
    My best code is written with the delete key.

  7. #7
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    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).
    Last edited by Frobozz; 09-09-2004 at 09:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob getting confused by booleans
    By Nathan the noob in forum C++ Programming
    Replies: 7
    Last Post: 06-27-2008, 03:50 PM
  2. 2d array of booleans
    By eklavya8 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2008, 02:36 PM
  3. Array of Booleans Represented with 1 Bit
    By Brad0407 in forum C++ Programming
    Replies: 43
    Last Post: 07-08-2007, 01:23 PM
  4. Help plz, Booleans
    By tagman in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2005, 08:30 AM
  5. booleans
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 03-10-2002, 12:01 PM