objective c - BOOL with 64-bit on iOS -


when use bool 32-bit, get:

bool b1=8960; //b1 == no  bool b2=8960; //b2 == true   

but 64-bit, get:

bool b1=8960; //b1 == yes  bool b2=8960; //b2 == true 

what has changed bool 32-bit 64-bit?

@timbodeit right, doesn't explain why ...

bool b1=8960; //b1 == no 

... evaluates no on 32-bit ios , why evaluates yes on 64-bit ios. let's start same beginning.

objc bool definition

#if (target_os_iphone && __lp64__)  ||  (__arm_arch_7k__ >= 2) #define objc_bool_is_bool 1 typedef bool bool; #else #define objc_bool_is_char 1 typedef signed char bool;  // bool explicitly signed @encode(bool) == "c" rather "c"  // if -funsigned-char used. #endif 

for 64-bit ios or armv7k (watch) it's defined bool , rest signed char.

objc bool yes , no

read objective-c literals, can find:

previously, bool type typedef signed char, , yes , no macros expand (bool)1 , (bool)0 respectively. support @yes , @no expressions, these macros defined using new language keywords in <objc/objc.h>:

#if __has_feature(objc_bool) #define yes __objc_yes #define no  __objc_no #else #define yes ((bool)1) #define no  ((bool)0) #endif 

the compiler implicitly converts __objc_yes , __objc_no (bool)1 , (bool)0. keywords used disambiguate bool , integer literals.

bool definition

bool macro defined in stdbool.h , expands _bool, boolean type introduced in c99. can store 2 values, 0 or 1. nothing else. more precise, stdbool.h defines 4 macros use:

/* don't define bool, true, , false in c++, except gnu extension. */ #ifndef __cplusplus #define bool _bool #define true 1 #define false 0 #elif defined(__gnuc__) && !defined(__strict_ansi__) /* define _bool, bool, false, true gnu extension. */ #define _bool bool #define bool  bool #define false false #define true  true #endif  #define __bool_true_false_are_defined 1 

_bool

_bool introduced in c99 , can hold values 0 or 1. what's important is:

when value demoted _bool, result 0 if value equals 0, , 1 otherwise.

now know mess comes , can better understand what's going on.

64-bit ios || armv7k

bool -> bool -> _bool (values 0 or 1)

demoting 8960 _bool gives 1, because value doesn't equal 0. see (_bool section).

32-bit ios

bool -> signed char (values -128 127).

if you're going store int values (-128 127) signed char, value unchanged per c99 6.3.1.3. otherwise implementation defined (c99 quote):

otherwise, new type signed , value cannot represented in it; either result implementation-defined or implementation-defined signal raised.

it means clang can decide. make short, default settings, clang wraps around (int -> signed char):

  • -129 becomes 127,
  • -130 becomes 126,
  • -131 becomes 125,
  • ...

and in opposite direction:

  • 128 becomes -128,
  • 129 becomes -127,
  • 130 becomes -126,
  • ...

but because signed char can store values in range -128 127, can store 0 well. example 256 (int) becomes 0 (signed char). , when value 8960 wrapped around ...

  • 8960 becomes 0,
  • 8961 becomes 1,
  • 8959 becomes -1,
  • ...

... becomes 0 when stored in signed char (8960 multiple of 256, 8960 % 256 == 0), it's no. same applies 256, 512, ... multiples of 256.

i recommend using yes, no bool , not relying on fancy c features int condition in if, etc. that's reason swift has bool, true, , false , can't use int values in conditions bool expected. avoid mess ...


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -