为什么0xF00解释为NO,当dec不为0时 [英] Why is 0xF00 interpreted as NO, when the dec is not 0

查看:273
本文介绍了为什么0xF00解释为NO,当dec不为0时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现

BOOL x = (BOOL)0xF00;

NO ...值 0xF00 不为零,但结果仍为 NO

is NO... the value 0xF00 is non zero, yet still the result is NO

但是这打击了我的范例,BOOL应该工作

but this blows my paradigm, that BOOL is supposed to work as

NO == 0, YES == any other value

为什么会这样?是否表示检查

Why is it like that? Does it mean that checking

if (object) {}

不安全?

推荐答案

BOOL 定义为一个有符号的char,即8位。但 0xF00 需要8位以上。因此,编译器采用最低的8位,其值为0.当我尝试它时,编译器专门警告这个问题:

BOOL is defined as a signed char, which is 8 bits. But 0xF00 requires more than 8 bits. So the compiler is taking the lowest 8 bits, which have a value of 0. When I try it, the compiler specifically warns about this problem:

warning: implicit conversion from 'int' to 'BOOL' (aka 'signed char') changes value from 3840 to 0 [-Wconstant-conversion]

如果你要为 BOOL 变量赋任意值,那么你的范例需要考虑值

If you're going to assign arbitrary values to BOOL variables, then your paradigm about needs to account for how values are represented.

这篇关于为什么0xF00解释为NO,当dec不为0时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆