 |
 |
ProjectBuilder booleans
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
Here's one for the truely hardcore
Under ProjectBuilder (or, under Objective-C specifically, if it matters) are the two following statements true?
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier><font color = green>nil</font> == <font color = green>NULL</font> == <font color = purple>NO</font> == <font color = purple>FALSE</font> == !<font color = purple>YES</font> == !<font color = purple>TRUE</font></font>[/code]
and
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>!<font color = green>nil</font> == !<font color = green>NULL</font> == !<font color = purple>NO</font> == !<font color = purple>FALSE</font> == <font color = purple>YES</font> == <font color = purple>TRUE</font></font>[/code]
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
!NULL.
(Well, that's AFAIK. I think YES and TRUE are defined to be 1 and the same type, and NULL/nil/NO/FALSE are defined to be 0)
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
id is defined as a pointer to a particular struct (one that contains just the isa variable), while nil is defined as (id)0 or (id)NULL, I believe. And NULL is often defined as (void*)0. The C standard permits null pointers for different types to have different internal representations, and they don't have to be all bits zero. This means that NULL and nil might have different internal representations.
(If NULL is defined as 0 as is commonly done, then talking about its internal representation as a null pointer makes no sense. It has to be given a type first.)
NO and FALSE are defined to be 0, I believe, and !YES and !TRUE evaluate to 0. This means that they all have the same bits...all bits 0.
That means that out of nil, NULL, NO, FALSE, !YES, and !TRUE only the last four are guaranteed to have identical bits.
None of that is of much concern, however, because C's type promotion rules guarantee that a 0 value will be promoted to the null pointer for the relevant type, and that pointers compare as the same type. This means that nil, NULL, NO, FALSE, !YES, and !TRUE will all evaluate as equal. Just don't use memcmp for comparison or memcpy for assignment on either of the first two with any of the others!
Regarding the second statement, I'm unsure as to what the unary negation (!) operator does to pointers, but I'm guessing that it evaluates to an int of value 1 if the pointer is the null pointer and 0 if it points to a valid object. In that case, !nil, !NULL, !NO, !FALSE, YES, and TRUE should all evaluate to be the same and have the same internal representations.
There, that's about as hardcore as I can get. There's probably lots of errors in what I said; feel free to correct me.
-Peter
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
I did some digging and testing and, as far as I can tell, you're completely right Wixar. Here's a summary:
These two statements *do* evaluate as true:
<font face = "courier">nil == NULL == NO == FALSE == !YES == !TRUE
!nil == !NULL == !NO == !FALSE == YES == TRUE</font>
nil is defined (according to Apple) type id.
NULL is defined (accoring to ANSI) type void*.
I'm not sure what YES, NO, TRUE, and FALSE are defined as.
Each of these statements evaluate as 0 (zero) when used in the context of BOOL:
<font face = "courier">nil, NULL, NO, FALSE, !YES, !TRUE</font>
Each of these statements evaluate as 1 when used in the context of BOOL:
<font face = "courier">!nil, !NULL, !NO, !FALSE, YES, TRUE</font>
Thanks for the responses guys 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|