Boolean Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Tuomi

    Boolean Values

    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

  • Mark A. Odell

    #2
    Re: Boolean Values

    Ian Tuomi <ianNOSPAM@co.j yu.fi> wrote in
    news:bmp1ko$o4n $1@phys-news1.kolumbus. fi:
    [color=blue]
    > 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.[/color]

    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 ->
    --

    Comment

    • Johan Aurér

      #3
      Re: Boolean Values

      On Fri, 17 Oct 2003, Mark A. Odell wrote:
      [color=blue]
      > 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;[/color]

      Only works if the implementation treats int bitfields as unsigned.

      --
      aurer@axis.com

      Comment

      • Irrwahn Grausewitz

        #4
        Re: Boolean Values

        Ian Tuomi <ianNOSPAM@co.j yu.fi> wrote:
        [color=blue]
        >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.[/color]

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

        Regards
        --
        Irrwahn
        (irrwahn33@free net.de)

        Comment

        • Derk Gwen

          #5
          Re: Boolean Values

          Ian Tuomi <ianNOSPAM@co.j yu.fi> 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.

          Comment

          • Tristan Miller

            #6
            Re: Boolean Values

            Greetings.

            In article <bmp1ko$o4n$1@p hys-news1.kolumbus. fi>, Ian Tuomi wrote:[color=blue]
            > 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.[/color]

            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. Microoptimizati ons 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

            Comment

            • Noah Roberts

              #7
              Re: Boolean Values

              Ian Tuomi wrote:
              [color=blue]
              > 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.[/color]

              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

              Comment

              • E. Robert Tisdale

                #8
                Re: Boolean Values

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

                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

                Comment

                • Christopher Benson-Manica

                  #9
                  Re: Boolean Values

                  E. Robert Tisdale <E.Robert.Tisda le@jpl.nasa.gov > spoke thus:
                  [color=blue]
                  > Don't use the stdbool.h header file.[/color]

                  What? Why not?

                  --
                  Christopher Benson-Manica | I *should* know what I'm talking about - if I
                  ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                  Comment

                  • Jeremy Yallop

                    #10
                    Re: Boolean Values

                    E. Robert Tisdale wrote:[color=blue]
                    > Ian Tuomi wrote:
                    >[color=green]
                    >> How can I define a boolean value in C?
                    >> (a value that can only be either 1 or 0)[/color]
                    >
                    > Don't use the stdbool.h header file.[/color]

                    Do you mean "Use the stdbool.h header"?
                    [color=blue]
                    > 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[/color]

                    Missing semicolon.
                    [color=blue]
                    > const bool false = 0;
                    > const bool true = !false;[/color]

                    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.
                    [color=blue]
                    > #endif _STDBOOL_H[/color]

                    The grammar doesn't allow tokens after #endif.

                    Jeremy.

                    Comment

                    • Goran Larsson

                      #11
                      Re: Boolean Values

                      In article <3F903C9A.70909 04@jpl.nasa.gov >,
                      E. Robert Tisdale <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                      [color=blue]
                      > Don't use the stdbool.h header file.[/color]

                      You have to motivate that recommendation.
                      [color=blue]
                      > If you don't have a stdbool.h header file, you can use[/color]

                      What should I do if I have a stdbool.h file? You just told us
                      not to use it.
                      [color=blue]
                      > #ifndef _STDBOOL_H
                      > #define _STDBOOL_H 1[/color]

                      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.
                      [color=blue]
                      > typedef int _Bool;[/color]

                      Once more are you threading on someone elses property.
                      [color=blue]
                      > typedef _Bool bool
                      > const bool false = 0;
                      > const bool true = !false;[/color]

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

                      --
                      Göran Larsson http://www.mitt-eget.com/

                      Comment

                      • Keith Thompson

                        #12
                        Re: Boolean Values

                        Ian Tuomi <ianNOSPAM@co.j yu.fi> writes:[color=blue]
                        > 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.[/color]

                        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_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
                        Schroedinger does Shakespeare: "To be *and* not to be"

                        Comment

                        • Ian Tuomi

                          #13
                          Re: Boolean Values

                          Keith Thompson wrote:
                          [color=blue]
                          > Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.[/color]

                          I looked there but it did not say how to save the memory. It only listed
                          the #define's for TRUE and FALSE.
                          [color=blue]
                          > 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.[/color]

                          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

                          Comment

                          • Jack Klein

                            #14
                            Re: Boolean Values

                            On Fri, 17 Oct 2003 18:59:52 +0200, Tristan Miller
                            <psychonaut@not hingisreal.com> wrote in comp.lang.c:
                            [color=blue]
                            > Greetings.
                            >
                            > In article <bmp1ko$o4n$1@p hys-news1.kolumbus. fi>, Ian Tuomi wrote:[color=green]
                            > > 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.[/color]
                            >
                            > According to the latest C standard, which your compiler may, may not, or may
                            > only partially support:
                            >
                            > #include <stdbool.h>
                            >
                            > _Bool foo;[/color]

                            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.
                            [color=blue]
                            > 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.[/color]

                            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.
                            [color=blue]
                            > If you're worried about memory and speed optimizations, concentrate on
                            > choosing efficient algorithms. Microoptimizati ons such as you propose are
                            > generally a waste of the programmer's time for all but the most constrained
                            > execution environments.
                            >
                            > Regards,
                            > Tristan[/color]

                            --
                            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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                            Comment

                            • Jack Klein

                              #15
                              Re: Boolean Values

                              On Fri, 17 Oct 2003 12:01:46 -0700, "E. Robert Tisdale"
                              <E.Robert.Tisda le@jpl.nasa.gov > wrote in comp.lang.c:
                              [color=blue]
                              > Ian Tuomi wrote:
                              >[color=green]
                              > > How can I define a boolean value in C?
                              > > (a value that can only be either 1 or 0)[/color]
                              >
                              > Don't use the stdbool.h header file.
                              > If you don't have a stdbool.h header file, you can use
                              >
                              > #ifndef _STDBOOL_H[/color]

                              Illegal invasion of namespace reserved for the implementation.
                              [color=blue]
                              > #define _STDBOOL_H 1
                              > typedef int _Bool;[/color]

                              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.
                              [color=blue]
                              > typedef _Bool bool
                              > const bool false = 0;
                              > const bool true = !false;
                              > #endif _STDBOOL_H[/color]

                              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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                              Comment

                              Working...