Boolean Values

I

Ian Tuomi

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
 
M

Mark A. Odell

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

=?ISO-8859-1?Q?Johan_Aur=E9r?=

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

Irrwahn Grausewitz

Ian Tuomi said:
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
 
D

Derk Gwen

# 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.
 
T

Tristan Miller

Greetings.

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
 
N

Noah Roberts

Ian said:
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
 
E

E. Robert Tisdale

Ian said:
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
 
G

Goran Larsson

E. Robert Tisdale said:
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.
 
J

Jeremy Yallop

E. Robert Tisdale said:
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.
 
K

Keith Thompson

Ian Tuomi said:
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.
 
I

Ian Tuomi

Keith said:

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
 
J

Jack Klein

Greetings.



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

Jack Klein

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

Keith Thompson

Jack Klein said:
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*.
 
K

Kevin Bracey

In message <[email protected]>
Jack Klein said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Question on switch 16
How to find out the size of an array? 28
What use is void? 23
Blue J Ciphertext Program 2
Standards 14
My Status, Ciphertext 2
How to play corresponding sound? 2
ChatGPT will make us Job(Home)less 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top