Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Boolean Values

Reply
Thread Tools

Boolean Values

 
 
Ian Tuomi
Guest
Posts: n/a
 
      10-17-2003
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

 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      10-17-2003
Ian Tuomi <(E-Mail Removed)> wrote in
news:bmp1ko$o4n$(E-Mail Removed):

> 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 ->
--
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Johan_Aur=E9r?=
Guest
Posts: n/a
 
      10-17-2003
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
Ian Tuomi <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      10-17-2003
Ian Tuomi <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      10-17-2003
Greetings.

In article <bmp1ko$o4n$(E-Mail Removed)>, 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
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-17-2003
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

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-17-2003
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

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-17-2003
E. Robert Tisdale <(E-Mail Removed)> 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.
 
Reply With Quote
 
Goran Larsson
Guest
Posts: n/a
 
      10-17-2003
In article <(E-Mail Removed)>,
E. Robert Tisdale <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
GridView: Change boolean values to Yes/No? Prozon ASP .Net 2 06-15-2007 05:32 AM
Using Boolean Values Paul ASP .Net 0 04-20-2006 12:41 PM
Unable get Boolean and Numeric values from XLS into Dataset and Datagrid vighnesh ASP .Net 3 08-04-2005 02:16 PM



Advertisments