gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?)


From: Pascal J . Bourguignon
Subject: Re: [RFA]: BOOL coding standards (Was: Problem with +numberWithBool: ?)
Date: Tue, 3 Feb 2004 00:13:30 +0100

Helge Hess writes:
> I would again like to point out that if the intention was to limit BOOL 
> to YES and NO, it would have been defined as
>    typedef enum { YES, NO } BOOL;

Absolutely not!

cat <<EOF >/tmp/pbs.c
#include <stdio.h>
typedef enum { no=0,yes } boole;
typedef unsigned char boolc;
#define YES ((boolc)1)
#define NO  ((boolc)0)
int main(void){ printf("sizeof enum bool = %d\nsizeof char bool = %d\n",
sizeof(boole),sizeof(boolc));return(0);}
EOF
gcc /tmp/pbs.c
./a.out

gives on a 32-bit host:

sizeof enum bool = 4
sizeof char bool = 1

It would  probably be even worse  and give sizeof  enum bool = 8  on a
64-bit host.

The only  reason why BOOL is defined  as a char is  because there's no
possibility in C (at the time when Objective-C was defined) to declare
a one bit type, so to limit the damage, they used a byte (char).


And, (while I  don't have my ANSI C reference at  hand), you can check
with  any C  compiler for  680x0 (at  least with  gcc), that  the code
generated by:

#include <stdio.h>
signed char   sb;
unsigned char ub;
int main(void){
    int a=1;int b=2-a;
    sb=a==b;ub=a==b;
    printf("sb=%d  ub=%d\n",sb,ub);
    return(0);
}

takes special care to leave both in sb and ub either 0 or 1:

        cmp.w -2(%a6),%d0
        seq %d0
        neg.b %d0

(That is, while  the native boolean values on  680x0 processor is 0xff
and 0xff, the gcc compiler use 0 and 1).

-- 
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]