<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Of BOOL and YES</title>
	<atom:link href="http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html/feed" rel="self" type="application/rss+xml" />
	<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html</link>
	<description>iOS and Objective-C Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Tue, 22 May 2012 11:53:47 -0500</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Jakob Heitz</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-53459</link>
		<dc:creator>Jakob Heitz</dc:creator>
		<pubDate>Sun, 19 Jun 2011 22:02:35 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-53459</guid>
		<description>The safe way to compare booleans is:
    (a==0) == (b==0)
or more compact:
    !a == !b</description>
		<content:encoded><![CDATA[<p>The safe way to compare booleans is:<br />
    (a==0) == (b==0)<br />
or more compact:<br />
    !a == !b</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joan Lluch</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-26813</link>
		<dc:creator>Joan Lluch</dc:creator>
		<pubDate>Thu, 30 Sep 2010 09:06:52 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-26813</guid>
		<description>Be very very careful with BOOLs. The worst scenario is not even mentioned in this thread. It is a common practice in C programing to use bitwise operators for checking conditions or states.

Consider the following code:

enum {
   kSomeConstant = 0x0100
};

BOOL test = ( 256 &amp; kSomeConstant ) ;
if ( test ) NSLog(@&quot;BOOL passed&quot;);
if ( ( 256 &amp; kSomeConstant ) ) NSLog(@&quot;int passed&quot;) ;

Only the second NSLog will be executed !. 

This is because &#039;test&#039; is actually a char type that can not hold the result of the operation, which is truncated to the least significant byte.

So always, ever (did I say at all times?) use *only* results of boolean operators to assign to BOOLs.

The solution for the code above is to change the test assignment by

BOOL test = ( 256 &amp; kSomeConstant ) != 0 ;

Joan Lluch-Zorrilla.</description>
		<content:encoded><![CDATA[<p>Be very very careful with BOOLs. The worst scenario is not even mentioned in this thread. It is a common practice in C programing to use bitwise operators for checking conditions or states.</p>
<p>Consider the following code:</p>
<p>enum {<br />
   kSomeConstant = 0&#215;0100<br />
};</p>
<p>BOOL test = ( 256 &amp; kSomeConstant ) ;<br />
if ( test ) NSLog(@&#8221;BOOL passed&#8221;);<br />
if ( ( 256 &amp; kSomeConstant ) ) NSLog(@&#8221;int passed&#8221;) ;</p>
<p>Only the second NSLog will be executed !. </p>
<p>This is because &#8216;test&#8217; is actually a char type that can not hold the result of the operation, which is truncated to the least significant byte.</p>
<p>So always, ever (did I say at all times?) use *only* results of boolean operators to assign to BOOLs.</p>
<p>The solution for the code above is to change the test assignment by</p>
<p>BOOL test = ( 256 &amp; kSomeConstant ) != 0 ;</p>
<p>Joan Lluch-Zorrilla.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel Colman</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-9138</link>
		<dc:creator>Michel Colman</dc:creator>
		<pubDate>Thu, 28 Jan 2010 07:30:04 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-9138</guid>
		<description>By the way, it is also perfectly safe to use &quot;true&quot; and &quot;false&quot; instead of the ugly upper case &quot;YES&quot; and &quot;NO&quot;. You won&#039;t find a single YES or NO in my code, and it works just fine. Whenever setting a BOOL to true, the compiler automagically inserts a YES (implicit conversion of BOOL to bool). The resulting code is the same, maybe it will add a millisecond or so to my total compile time, and I guess a few bytes to the size of my code since YES and NO are shorter words, but otherwise it makes zero difference.</description>
		<content:encoded><![CDATA[<p>By the way, it is also perfectly safe to use &#8220;true&#8221; and &#8220;false&#8221; instead of the ugly upper case &#8220;YES&#8221; and &#8220;NO&#8221;. You won&#8217;t find a single YES or NO in my code, and it works just fine. Whenever setting a BOOL to true, the compiler automagically inserts a YES (implicit conversion of BOOL to bool). The resulting code is the same, maybe it will add a millisecond or so to my total compile time, and I guess a few bytes to the size of my code since YES and NO are shorter words, but otherwise it makes zero difference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel Colman</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-9137</link>
		<dc:creator>Michel Colman</dc:creator>
		<pubDate>Thu, 28 Jan 2010 07:21:02 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-9137</guid>
		<description>It is perfectly safe to use
bool b = myBOOL;
and
myBOOL = b;

These are exactly equivalent to your complicated question mark constructions. For example:

bool b = 3.141592654;
will set b to true. Conversion of anything to bool will set it to true if the original was not zero, and to false otherwise. No need to use &quot;? true: false&quot; because that&#039;s what a conversion to bool does by definition.

In the other direction, a bool will always be converted to 0 or 1, which corresponds to NO and YES in the BOOL type, so no worries there, either.

An &quot;if&quot; condition is also implicitly converted to bool, so the same applies there.

The only thing you should avoid, is explicitly comparing to YES.</description>
		<content:encoded><![CDATA[<p>It is perfectly safe to use<br />
bool b = myBOOL;<br />
and<br />
myBOOL = b;</p>
<p>These are exactly equivalent to your complicated question mark constructions. For example:</p>
<p>bool b = 3.141592654;<br />
will set b to true. Conversion of anything to bool will set it to true if the original was not zero, and to false otherwise. No need to use &#8220;? true: false&#8221; because that&#8217;s what a conversion to bool does by definition.</p>
<p>In the other direction, a bool will always be converted to 0 or 1, which corresponds to NO and YES in the BOOL type, so no worries there, either.</p>
<p>An &#8220;if&#8221; condition is also implicitly converted to bool, so the same applies there.</p>
<p>The only thing you should avoid, is explicitly comparing to YES.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-260</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 27 Feb 2009 16:14:49 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-260</guid>
		<description>That works fine. Basically the compiler is comparing to zero, which is why you can do stuff like:

char* cp = some_address;
if (cp) {
// do something if cp != 0
}

if (!cp) {
// do something if cp == 0
}

So, the magic is in the ! operator. Given:

! value 

the result is of type int and is the &quot;logical negation&quot; of the value:

0 if value is nonzero;
1 if value is 0.

You can also say that the expression:

! value

is equivalent to:

(0 == value)

Notice that this explicitly avoids comparing with YES (or 1), which was the main point of my original note.

HTH!</description>
		<content:encoded><![CDATA[<p>That works fine. Basically the compiler is comparing to zero, which is why you can do stuff like:</p>
<p>char* cp = some_address;<br />
if (cp) {<br />
// do something if cp != 0<br />
}</p>
<p>if (!cp) {<br />
// do something if cp == 0<br />
}</p>
<p>So, the magic is in the ! operator. Given:</p>
<p>! value </p>
<p>the result is of type int and is the &#8220;logical negation&#8221; of the value:</p>
<p>0 if value is nonzero;<br />
1 if value is 0.</p>
<p>You can also say that the expression:</p>
<p>! value</p>
<p>is equivalent to:</p>
<p>(0 == value)</p>
<p>Notice that this explicitly avoids comparing with YES (or 1), which was the main point of my original note.</p>
<p>HTH!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: secretsquirrel</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-258</link>
		<dc:creator>secretsquirrel</dc:creator>
		<pubDate>Fri, 27 Feb 2009 04:14:55 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-258</guid>
		<description>a question: 
in objective-c and iphone development, 
do both BOOL and bool allow setting with ! to toggle to the opposite?


example:
myBOOL = !myBOOL;
newBOOL = !myBOOL;

and so on.

can i pass a message to a method with !myBOOL?</description>
		<content:encoded><![CDATA[<p>a question:<br />
in objective-c and iphone development,<br />
do both BOOL and bool allow setting with ! to toggle to the opposite?</p>
<p>example:<br />
myBOOL = !myBOOL;<br />
newBOOL = !myBOOL;</p>
<p>and so on.</p>
<p>can i pass a message to a method with !myBOOL?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pRAMOD</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-228</link>
		<dc:creator>pRAMOD</dc:creator>
		<pubDate>Fri, 13 Feb 2009 05:04:36 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-228</guid>
		<description>Good post......most usefull !!</description>
		<content:encoded><![CDATA[<p>Good post&#8230;&#8230;most usefull !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adolfo</title>
		<link>http://MobileDeveloperTips.com/objective-c/of-bool-and-yes.html#comment-172</link>
		<dc:creator>adolfo</dc:creator>
		<pubDate>Sat, 17 Jan 2009 07:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422#comment-172</guid>
		<description>Great post, man.  Concise and very useful.  It&#039;ll probably save me hours of frustrating debugging in the future.</description>
		<content:encoded><![CDATA[<p>Great post, man.  Concise and very useful.  It&#8217;ll probably save me hours of frustrating debugging in the future.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

