布尔真 - 正1或负1? [英] Boolean true - positive 1 or negative 1?
问题描述
我设计的语言,并试图决定是否真正
应为0x01或0xFF的。显然,所有非零值将被转换为真实的,但我想的确切内部重新presentation决定。
I'm designing a language, and trying to decide whether true
should be 0x01 or 0xFF. Obviously, all non-zero values will be converted to true, but I'm trying to decide on the exact internal representation.
什么是每个选择的利弊?
What are the pros and cons for each choice?
推荐答案
0是假,因为处理器具有当一个寄存器被设置到零,它被设置一个标志。
0 is false because the processor has a flag that is set when a register is set to zero.
没有其他标志设置的任何其他值(0×01,为0xFF等) - 但零标志设置为false时,有在寄存器非零值
No other flags are set on any other value (0x01, 0xff, etc) - but the zero flag is set to false when there's a non-zero value in the register.
因此,这里崇尚定义0虚假的答案和其他任何为真都是正确的。
So the answers here advocating defining 0 as false and anything else as true are correct.
如果你想界定真正的默认值,然后0x01为比大多数:
If you want to "define" a default value for true, then 0x01 is better than most:
- 这再次presents在每一个位长度和符号性 相同数量
- 它只需要测试一个位,如果你想知道它是否是真的,应该零标志不可用,或昂贵的使用
- 无需转换为其他类型时不用担心符号扩展
- 逻辑和算术前pressions采取同样它
这篇关于布尔真 - 正1或负1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!