Boolean Values

Discussion in 'C Programming' started by Ian Tuomi, Oct 17, 2003.

  1. Ian Tuomi

    Ian Tuomi Guest

    How can I define a boolean value in c? (an value that can only be either
    1 or 0) I feel bad for the memory loss when declaring ints for variables
    that do not need that much memory.
    --
    Ian Tuomi
    Jyväskylä, Finland

    "Very funny scotty, now beam down my clothes."

    GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
    !O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

    NOTE: Remove NOSPAM from address
     
    Ian Tuomi, Oct 17, 2003
    #1
    1. Advertising

  2. Ian Tuomi <> wrote in
    news:bmp1ko$o4n$:

    > How can I define a boolean value in c? (an value that can only be either
    > 1 or 0) I feel bad for the memory loss when declaring ints for variables
    > that do not need that much memory.


    You can't do what I think you really want in C, this will give you
    something that can only hold a one or a zero:

    struct MyBool
    {
    int boolean : 1;
    };

    struct MyBool flag;

    flag.boolean = 1;

    This still uses a full int though. If you really want to have a boolean
    that uses one bit only then use an 8051 with a C compiler in non-standard
    mode and declare your booleans with the 'bit' type.


    --
    - Mark ->
    --
     
    Mark A. Odell, Oct 17, 2003
    #2
    1. Advertising

  3. On Fri, 17 Oct 2003, Mark A. Odell wrote:

    > You can't do what I think you really want in C, this will give you
    > something that can only hold a one or a zero:
    >
    > struct MyBool
    > {
    > int boolean : 1;
    > };
    >
    > struct MyBool flag;
    >
    > flag.boolean = 1;


    Only works if the implementation treats int bitfields as unsigned.

    --
     
    =?ISO-8859-1?Q?Johan_Aur=E9r?=, Oct 17, 2003
    #3
  4. Ian Tuomi <> wrote:

    >How can I define a boolean value in c? (an value that can only be either
    >1 or 0) I feel bad for the memory loss when declaring ints for variables
    >that do not need that much memory.


    Unless you are writing code for a platform with extremely limited
    resources you shouldn't bother at all.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #4
  5. Ian Tuomi

    Derk Gwen Guest

    Ian Tuomi <> wrote:
    # How can I define a boolean value in c? (an value that can only be either
    # 1 or 0) I feel bad for the memory loss when declaring ints for variables
    # that do not need that much memory.

    I generally use
    typedef unsigned char bool;
    enum {true=1,false=0};
    and don't worry about how tightly it's packed. I got 256Mbytes of real memory
    and gigabytes of virtual.

    If you have a number of booleans in one structure, you can pack them
    one bit at a time,
    struct {
    int empty:1;
    int nullable:1;
    int productive:1;
    int weight:7;
    int leftrcr:1;
    int rightrcr:1;
    int embedding:1;
    ...
    }
    which can trade space for time.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I think that's kinda of personal; I don't think I should answer that.
     
    Derk Gwen, Oct 17, 2003
    #5
  6. Greetings.

    In article <bmp1ko$o4n$>, Ian Tuomi wrote:
    > How can I define a boolean value in c? (an value that can only be either
    > 1 or 0) I feel bad for the memory loss when declaring ints for variables
    > that do not need that much memory.


    According to the latest C standard, which your compiler may, may not, or may
    only partially support:

    #include <stdbool.h>

    _Bool foo;

    Note that using the new boolean type doesn't guarantee that the compiler is
    actually going to set aside just one bit of memory for the variable. In
    fact, it probably won't. In practice, the low-level machine code
    operations required to test individual bits are typically slower than those
    used to compare whole words anyway. So even if you could squeeze your
    boolean variables down to a single bit in size, you'd be trading off
    execution speed.

    If you're worried about memory and speed optimizations, concentrate on
    choosing efficient algorithms. Microoptimizations such as you propose are
    generally a waste of the programmer's time for all but the most constrained
    execution environments.

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Oct 17, 2003
    #6
  7. Ian Tuomi

    Noah Roberts Guest

    Ian Tuomi wrote:

    > How can I define a boolean value in c? (an value that can only be either
    > 1 or 0) I feel bad for the memory loss when declaring ints for variables
    > that do not need that much memory.


    The problem is that in todays computers access to single bits isn't
    really available. You work with entiry bytes or sometimes larger, use
    and/or/xor, and compare with 0. Therefor there is no way to really do
    this unless you have more than one boolean value you want to hold. If
    that is the case you use bit manipulations to set and unset bits on
    something like a u_char or whatever.

    NR
     
    Noah Roberts, Oct 17, 2003
    #7
  8. Ian Tuomi wrote:

    > How can I define a boolean value in C?
    > (a value that can only be either 1 or 0)


    Don't use the stdbool.h header file.
    If you don't have a stdbool.h header file, you can use

    #ifndef _STDBOOL_H
    #define _STDBOOL_H 1
    typedef int _Bool;
    typedef _Bool bool
    const bool false = 0;
    const bool true = !false;
    #endif _STDBOOL_H
     
    E. Robert Tisdale, Oct 17, 2003
    #8
  9. E. Robert Tisdale <> spoke thus:

    > Don't use the stdbool.h header file.


    What? Why not?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 17, 2003
    #9
  10. In article <>,
    E. Robert Tisdale <> wrote:

    > Don't use the stdbool.h header file.


    You have to motivate that recommendation.

    > If you don't have a stdbool.h header file, you can use


    What should I do if I have a stdbool.h file? You just told us
    not to use it.

    > #ifndef _STDBOOL_H
    > #define _STDBOOL_H 1


    You are invading the implementations namespace by using a name
    that starts with an underscore followed by an upper case character.
    See paragraph 7.1.3 in the ANSI/ISO/IEC 9899-1999 standard.

    > typedef int _Bool;


    Once more are you threading on someone elses property.

    > typedef _Bool bool
    > const bool false = 0;
    > const bool true = !false;


    Using ``!false'' has no advantages over using ``1'', only disadvantages.

    --
    Göran Larsson http://www.mitt-eget.com/
     
    Goran Larsson, Oct 17, 2003
    #10
  11. E. Robert Tisdale wrote:
    > Ian Tuomi wrote:
    >
    >> How can I define a boolean value in C?
    >> (a value that can only be either 1 or 0)

    >
    > Don't use the stdbool.h header file.


    Do you mean "Use the stdbool.h header"?

    > If you don't have a stdbool.h header file, you can use
    >
    > #ifndef _STDBOOL_H
    > #define _STDBOOL_H 1
    > typedef int _Bool;
    > typedef _Bool bool


    Missing semicolon.

    > const bool false = 0;
    > const bool true = !false;


    There are at least two serious problems with these definitions:

    * The values of `false' and `true' cannot be used in constant
    expressions.

    * The header cannot be used in multiple source files. Using these
    variables in more than one translation unit causes undefined
    behaviour, due to multiple external definitions for `false' and
    `true'.

    Further, there doesn't seem to be a good reason for `true' and `false'
    to be addressable.

    Arguments could be made for using either an enum or macros, but const
    variables are not the right way to do this. The standards people knew
    what they were doing when they made `false' and `true' macros, you
    know. In particular, they understood the difference between the way
    `const' works in C and in C++.

    Also, note that the _Bool defined here doesn't behave much like the
    standard _Bool; I'd call it something else to avoid possible
    confusion.

    > #endif _STDBOOL_H


    The grammar doesn't allow tokens after #endif.

    Jeremy.
     
    Jeremy Yallop, Oct 17, 2003
    #11
  12. Ian Tuomi <> writes:
    > How can I define a boolean value in c? (an value that can only be either
    > 1 or 0) I feel bad for the memory loss when declaring ints for variables
    > that do not need that much memory.


    Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.

    Don't worry too much about using up extra memory. On many systems,
    using a variable smaller than a word will cost you more in extra code
    size than you save in data size.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Oct 17, 2003
    #12
  13. Ian Tuomi

    Ian Tuomi Guest

    Keith Thompson wrote:

    > Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.


    I looked there but it did not say how to save the memory. It only listed
    the #define's for TRUE and FALSE.

    > Don't worry too much about using up extra memory. On many systems,
    > using a variable smaller than a word will cost you more in extra code
    > size than you save in data size.


    Ok, I just felt bad for all the memory going to waste but I guess that
    is irrelevant taking into consideration the current state of computers.
    Thanks Everyone.

    --
    Ian Tuomi
    Jyväskylä, Finland

    "Very funny scotty, now beam down my clothes."

    NOTE: Remove NOSPAM from address
     
    Ian Tuomi, Oct 17, 2003
    #13
  14. Ian Tuomi

    Jack Klein Guest

    On Fri, 17 Oct 2003 18:59:52 +0200, Tristan Miller
    <> wrote in comp.lang.c:

    > Greetings.
    >
    > In article <bmp1ko$o4n$>, Ian Tuomi wrote:
    > > How can I define a boolean value in c? (an value that can only be either
    > > 1 or 0) I feel bad for the memory loss when declaring ints for variables
    > > that do not need that much memory.

    >
    > According to the latest C standard, which your compiler may, may not, or may
    > only partially support:
    >
    > #include <stdbool.h>
    >
    > _Bool foo;


    Actually, no header is required to use _Bool, which is a built-in
    type. <stdbool.h> only defines the macros bool (as a synonym for
    _Bool), and the values true and false.

    > Note that using the new boolean type doesn't guarantee that the compiler is
    > actually going to set aside just one bit of memory for the variable. In
    > fact, it probably won't. In practice, the low-level machine code
    > operations required to test individual bits are typically slower than those
    > used to compare whole words anyway. So even if you could squeeze your
    > boolean variables down to a single bit in size, you'd be trading off
    > execution speed.


    Actually _Bool can't use individual bytes, it is an unsigned integer
    type. You can have arrays of _Bool and take the address of a _Bool.
    Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
    must occupy at least as much memory as one of the character types.

    > If you're worried about memory and speed optimizations, concentrate on
    > choosing efficient algorithms. Microoptimizations such as you propose are
    > generally a waste of the programmer's time for all but the most constrained
    > execution environments.
    >
    > Regards,
    > Tristan


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Oct 18, 2003
    #14
  15. Ian Tuomi

    Jack Klein Guest

    On Fri, 17 Oct 2003 12:01:46 -0700, "E. Robert Tisdale"
    <> wrote in comp.lang.c:

    > Ian Tuomi wrote:
    >
    > > How can I define a boolean value in C?
    > > (a value that can only be either 1 or 0)

    >
    > Don't use the stdbool.h header file.
    > If you don't have a stdbool.h header file, you can use
    >
    > #ifndef _STDBOOL_H


    Illegal invasion of namespace reserved for the implementation.

    > #define _STDBOOL_H 1
    > typedef int _Bool;


    Guaranteed syntax error on any compiler that supports the standard C
    type _Bool, because _Bool is a built-in type and a keyword. The
    header <stdbool.h> is not needed to use the _Bool type on a conforming
    compiler.

    > typedef _Bool bool
    > const bool false = 0;
    > const bool true = !false;
    > #endif _STDBOOL_H


    Of course, in many, many ways, this does not act at all like the
    standard C type _Bool.

    The standard _Bool type is an unsigned type and works like this:

    #include <stdbool.h> /* for macros true and false */

    _Bool b1 = true;

    --b1; /* b1 is now false */
    --b1; /* b1 is now true */
    --b1; /* b1 is now false again! */

    ....try that with your lame typedef.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Oct 18, 2003
    #15
  16. Jack Klein <> writes:
    [...]
    > Actually _Bool can't use individual bytes, it is an unsigned integer
    > type. You can have arrays of _Bool and take the address of a _Bool.
    > Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
    > must occupy at least as much memory as one of the character types.


    Typo alert: _Bool can't use individual *bits*.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Oct 18, 2003
    #16
  17. Ian Tuomi

    Kevin Bracey Guest

    In message <>
    Jack Klein <> wrote:

    > Actually _Bool can't use individual bits, it is an unsigned integer
    > type. You can have arrays of _Bool and take the address of a _Bool.
    > Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
    > must occupy at least as much memory as one of the character types.


    That's only true when you're actually manipulating them in memory. If you had
    a _Bool variable that never had its address taken, a smart
    compiler would be able to store it in a single bit of memory or of a CPU
    register.

    And a calling standard could specify that functions returning _Bool actually
    returned the result in a CPU's Zero flag, rather than in a general purpose
    register - a potentially significant space and speed optimisation.

    _Bool allows a number of cunning tricks like that. It just doesn't allow
    arrays of bools to be packed bits, which is a shame.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Oct 20, 2003
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prozon
    Replies:
    2
    Views:
    8,851
    sushil.jinder
    Jun 15, 2007
  2. vighnesh
    Replies:
    3
    Views:
    1,035
    Paul Clement
    Aug 4, 2005
  3. Paul

    Using Boolean Values

    Paul, Apr 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    468
  4. J Leonard
    Replies:
    4
    Views:
    12,829
    Mark Space
    Jan 19, 2008
  5. Metre Meter
    Replies:
    7
    Views:
    428
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page