Created
May 7, 2012 19:14
-
-
Save markd2/2629780 to your computer and use it in GitHub Desktop.
Playing with different scenarios with Objective-C's BOOL type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// clang -g -Wall -framework Foundation -o bool bool.m | |
// Are these two integers different? YES if so, NO if not | |
// (even though it says YES and NO, it LIES! Can actualy return a range | |
// of non-YES values) | |
static BOOL different (int thing1, int thing2) { | |
return thing1 - thing2; | |
} // difference | |
// Are these two integers different? true if so, false if not | |
// Thiso one is actually reliable. | |
static bool std_different (int thing1, int thing2) { | |
return thing1 - thing2; | |
} // difference | |
static BOOL same (int thing1, int thing2) { | |
return !(thing1 - thing2); // logical expression always 0 or 1 | |
} // difference | |
static NSString *g_name; | |
static BOOL haveName () { | |
return (BOOL)g_name; | |
} // haveName | |
static bool stdHaveName () { | |
return (bool)g_name; | |
} // stdHaveName | |
int main (void) { | |
printf ("Slice\n"); | |
BOOL truefalse; | |
truefalse = (BOOL)256; | |
printf ("256 -> %d (BOOL)\n", truefalse); | |
bool stdTruefalse; | |
stdTruefalse = (bool)256; | |
printf ("256 -> %d (bool)\n", stdTruefalse); | |
printf ("\nDifference\n"); | |
if (different(11, 10) == YES) printf ("11 != 10\n"); | |
else printf ("11 == 10\n"); | |
if (different(10, 11) == YES) printf ("10 != 11\n"); | |
else printf ("10 == 11\n"); | |
if (different(10, 15) == YES) printf ("10 != 15\n"); | |
else printf ("10 == 15\n"); | |
if (different(512, 256) == YES) printf ("512 != 256\n"); | |
else printf ("512 == 256\n"); | |
printf ("%d %d %d %d\n", | |
different(11, 10), different(10, 11), | |
different(10, 15), different(512, 256)); | |
printf ("\nStd Difference\n"); | |
if (std_different(10, 11) == YES) printf ("10 != 11\n"); | |
else printf ("10 == 11\n"); | |
if (std_different(11, 10) == YES) printf ("11 != 10\n"); | |
else printf ("11 == 10\n"); | |
if (std_different(10, 15) == YES) printf ("10 != 15\n"); | |
else printf ("10 == 15\n"); | |
if (std_different(512, 256) == YES) printf ("512 != 256\n"); | |
else printf ("512 == 256\n"); | |
printf ("\nSame Difference\n"); | |
if (same(10, 11) == YES) printf ("10 == 11\n"); | |
else printf ("10 != 11\n"); | |
if (same(11, 10) == YES) printf ("11 == 10\n"); | |
else printf ("11 != 10\n"); | |
if (same(512, 256) == YES) printf ("512 == 256\n"); | |
else printf ("512 != 256\n"); | |
printf ("\nAddress (BOOL)\n"); | |
g_name = nil; | |
if (haveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
g_name = (NSString *)0x12345678; | |
if (haveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
g_name = (NSString *)0x12345600; | |
if (haveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
printf ("\nAddress (std)\n"); | |
g_name = nil; | |
if (stdHaveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
g_name = (NSString *)0x12345678; | |
if (stdHaveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
g_name = (NSString *)0x12345600; | |
if (stdHaveName()) printf ("%p means name set\n", g_name); | |
else printf ("%p means name not set\n", g_name); | |
return 0; | |
} // main | |
#if SAMPLE_RUN | |
% ./bool | |
Slice | |
256 -> 0 (BOOL) | |
256 -> 1 (bool) | |
Difference | |
open -11 != 10 | |
10 == 11 | |
10 == 15 | |
512 == 256 | |
1 -1 -5 0 | |
Std Difference | |
10 != 11 | |
11 != 10 | |
10 != 15 | |
512 != 256 | |
Same Difference | |
10 != 11 | |
11 != 10 | |
512 != 256 | |
Address (BOOL) | |
0x0 means name not set | |
0x12345678 means name set | |
0x12345600 means name not set | |
Address (std) | |
0x0 means name not set | |
0x12345678 means name set | |
0x12345600 means name set | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment