布尔值为true - 正1还是负1?

时间:2022-04-11 05:31:13

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.

我正在设计一种语言,并试图确定true应该是0x01还是0xFF。显然,所有非零值都将转换为true,但我正在尝试确定精确的内部表示。

What are the pros and cons for each choice?

每种选择的优缺点是什么?

11 个解决方案

#1


0 is false because the processor has a flag that is set when a register is set to zero.

0为假,因为处理器具有在寄存器设置为零时设置的标志。

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.

没有其他标志设置在任何其他值(0x01,0xff等)上 - 但当寄存器中存在非零值时,零标志设置为false。

So the answers here advocating defining 0 as false and anything else as true are correct.

所以这里的答案主张将0定义为false,将其他任何内容定义为true都是正确的。

If you want to "define" a default value for true, then 0x01 is better than most:

如果要“定义”默认值为true,则0x01优于大多数:

  • It represents the same number in every bit length and signedness
  • 它表示每个位长度和符号的相同数字

  • It only requires testing one bit if you want to know whether it's true, should the zero flag be unavailable, or costly to use
  • 如果你想知道它是否属实,零标志是否不可用或者使用成本高,它只需要测试一位

  • No need to worry about sign extension during conversion to other types
  • 转换为其他类型时无需担心签名扩展

  • Logical and arithmetic expressions act the same on it
  • 逻辑和算术表达式对其起作用相同

-Adam

#2


It doesn't matter, as long as it satisfies the rules for the external representation.

无关紧要,只要它满足外部表示的规则即可。

I would take a hint from C here, where false is defined absolutely as 0, and true is defined as not false. This is an important distinction, when compared to an absolute value for true. Unless you have a type that only has two states, you have to account for all values within that value type, what is true, and what is false.

我会在这里从C中得到一个提示,其中false绝对定义为0,而true定义为非假。与真实的绝对值相比,这是一个重要的区别。除非您的类型只有两种状态,否则您必须考虑该值类型中的所有值,什么是真,哪些是假。

#3


Why are you choosing that non-zero values are true? In Ada true is TRUE and false is FALSE. There is no implicit type conversion to and from BOOLEAN.

你为什么选择非零值是真的?在Ada中,true为TRUE,false为FALSE。 BOOLEAN没有隐式类型转换。

#4


Using -1 has one advantage in a weakly typed language -- if you mess up and use the bitwise and operator instead of the logical and operator, your condition will still evaluate correctly as long as one of the operands has been converted to the canonical boolean representation. This isn't true if the canonical representation is 1.

使用-1在弱类型语言中有一个优点 - 如果你陷入困境并使用按位和运算符而不是逻辑和运算符,只要其中一个操作数已转换为规范布尔值,您的条件仍将正确评估表示。如果规范表示为1,则不是这样。

  0xffffffff & 0x00000010 == 0x00000010 (true)
  0xffffffff && 0x00000010 == 0xffffffff (true)

but

  0x00000001 & 0x00000010 == 0x00000000 (false)
  0x00000001 && 0x00000010 == 0xffffffff (true)

#5


IMO, if you want to stick with false=0x00, you should use 0x01. 0xFF is usually:

IMO,如果你想坚持假= 0x00,你应该使用0x01。 0xFF通常是:

  • a sign that some operation overflowed
  • 某些操作溢出的迹象

or

  • an error marker
  • 错误标记

And in both cases, it probably means false. Hence the *nix return value convention from executables, that true=0x00, and any non-zero value is false.

在这两种情况下,它可能意味着错误。因此,来自可执行文件的* nix返回值约定,即true = 0x00,并且任何非零值都是false。

#6


-1 is longer to type than 1...

-1比1更长的类型...

In the end it doesn't matter since 0 is false and anything else is true, and you will never compare to the exact representation of true.

最后它没关系,因为0是假的,其他任何事情都是真的,你永远不会与真实的确切表示进行比较。

Edit, for those down voting, please explain why. This answer is essentially the same as the one currently rated at +19. So that is 21 votes difference for what is the same basic answer.

编辑,对于那些投票,请解释原因。这个答案基本上与目前评级为+19的答案相同。所以这是相同的基本答案的21票差异。

If it is because of the -1 comment, it is true, the person who actually defines "true" (eg: the compiler writer) is going to have to use -1 instead of 1, assuming they chose to use an exact representation. -1 is going to take longer to type than 1, and the end result will be the same. The statement is silly, it was meant to be silly, because there is no real difference between the two (1 or -1).

如果是因为-1注释,那么实际定义“true”的人(例如:编译器编写者)将不得不使用-1而不是1,假设他们选择使用精确表示。 -1输入的时间比1要长,最终结果是相同的。声明很愚蠢,它本来是愚蠢的,因为两者之间没有真正的区别(1或-1)。

If you are going to mark something down at least provide a rationale for it.

如果你要标记某些东西至少提供一个理由。

#7


0xff is an odd choice since it has an implicit assumption that 8 bits is your minimum storage unit. But it's not that uncommon to want to store boolean values more compactly than that.

0xff是一个奇怪的选择,因为它隐含假设8位是你的最小存储单元。但是,想要比这更紧凑地存储布尔值并不常见。

Perhaps you want to rephrase by thinking about whether boolean operators produce something that is just one 0 or 1 bit (which works regardless of sign extension), or is all-zeroes or all-ones (and depends on sign extension of signed two's-complement quantities to maintain all-ones at any length).

也许你想通过考虑布尔运算符是否只产生一个0或1位(无论符号扩展是否有效),或者是全零或全1(并且取决于有符号二进制补码的符号扩展)来改写。在任何长度维持所有的数量)。

I think your life is simpler with 0 and 1.

我认为你的生活更简单,0和1。

#8


The pros are none, and the cons are none, too. As long as you provide an automatic conversion from integer to boolean, it will be arbitrary, so it really doesn't matter which numbers you choose.

专业人士都没有,缺点也没有。只要你提供从整数到布尔值的自动转换,它就是任意的,所以你选择哪个数字并不重要。

On the other hand, if you didn't allow this automatic conversion you'd have a pro: you wouldn't have some entirely arbitrary rule in your language. You wouldn't have (7 - 4 - 3) == false, or 3 * 4 + 17 == "Hello", or "Hi mom!" == Complex(7, -2).

另一方面,如果你不允许这种自动转换,你就有了专业人士:你的语言中没有完全任意的规则。你不会有(7 - 4 - 3)== false,或3 * 4 + 17 ==“你好”,或“嗨妈妈!” ==复杂(7,-2)。

#9


I think the C method is the way to go. 0 means false, anything else means true. If you go with another mapping for true, then you are left with the problem of having indeterminate values - that are neither true nor false.

我认为C方法是可行的方法。 0表示错误,其他任何表示都是真的。如果您使用另一个映射为true,那么您将面临具有不确定值的问题 - 既不是真也不是假。

If this is language that you'll be compiling for a specific instruction set that has special support for a particular representation, then I'd let that guide you. But absent any additional information, for an 'standard' internal representation, I'd go with -1 (all 1's in binary). This value extends well to whatever size boolean you want (single bit, 8-bit, 16, etc), and if you break up a "TRUE" or a "FALSE" into a smaller "TRUE" or "FALSE", its still the same. (where if you broke a 16 bit TRUE=0x0001 you'd get a FALSE=0x00 and a TRUE=0x01).

如果这是您将为特定表示特别支持的特定指令集编译的语言,那么我将指导您。但是如果没有任何附加信息,对于“标准”内部表示,我会使用-1(二进制中的所有1)。这个值很好地扩展到你想要的任何大小的布尔值(单位,8位,16等),如果你将“TRUE”或“FALSE”分解为更小的“TRUE”或“FALSE”,它仍然相同。 (如果你打破了16位TRUE = 0x0001,你会得到一个FALSE = 0x00和一个TRUE = 0x01)。

#10


Design the language so that 0 is false and non-zero is true. There is no need to "convert" anything, and thinking "non-zero" instead of some specific value will help you write the code properly.

设计语言,使0为假,非零为真。没有必要“转换”任何东西,并且认为“非零”而不是某些特定值将帮助您正确编写代码。

If you have built-in symbols like "True" then go ahead and pick a value, but always think "non-zero is true" instead of "0x01 is true".

如果你有像“True”这样的内置符号,那么继续选择一个值,但总是认为“非零为真”而不是“0x01为真”。

#11


Whatever you do, once you select your values don't change them. In FORTH-77, true and false were defined as 1 and 0. Then, FORTH-83 redefined them as -1 and 0. There were a not few (well ok, only a few, this is FORTH we are talking about) problems caused by this.

无论你做什么,一旦你选择了你的价值观,就不要改变它们。在FORTH-77中,true和false被定义为1和0.然后,FORTH-83将它们重新定义为-1和0.有一些(很好,只有少数,这是我们正在讨论的问题)问题由此引起的。

#1


0 is false because the processor has a flag that is set when a register is set to zero.

0为假,因为处理器具有在寄存器设置为零时设置的标志。

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.

没有其他标志设置在任何其他值(0x01,0xff等)上 - 但当寄存器中存在非零值时,零标志设置为false。

So the answers here advocating defining 0 as false and anything else as true are correct.

所以这里的答案主张将0定义为false,将其他任何内容定义为true都是正确的。

If you want to "define" a default value for true, then 0x01 is better than most:

如果要“定义”默认值为true,则0x01优于大多数:

  • It represents the same number in every bit length and signedness
  • 它表示每个位长度和符号的相同数字

  • It only requires testing one bit if you want to know whether it's true, should the zero flag be unavailable, or costly to use
  • 如果你想知道它是否属实,零标志是否不可用或者使用成本高,它只需要测试一位

  • No need to worry about sign extension during conversion to other types
  • 转换为其他类型时无需担心签名扩展

  • Logical and arithmetic expressions act the same on it
  • 逻辑和算术表达式对其起作用相同

-Adam

#2


It doesn't matter, as long as it satisfies the rules for the external representation.

无关紧要,只要它满足外部表示的规则即可。

I would take a hint from C here, where false is defined absolutely as 0, and true is defined as not false. This is an important distinction, when compared to an absolute value for true. Unless you have a type that only has two states, you have to account for all values within that value type, what is true, and what is false.

我会在这里从C中得到一个提示,其中false绝对定义为0,而true定义为非假。与真实的绝对值相比,这是一个重要的区别。除非您的类型只有两种状态,否则您必须考虑该值类型中的所有值,什么是真,哪些是假。

#3


Why are you choosing that non-zero values are true? In Ada true is TRUE and false is FALSE. There is no implicit type conversion to and from BOOLEAN.

你为什么选择非零值是真的?在Ada中,true为TRUE,false为FALSE。 BOOLEAN没有隐式类型转换。

#4


Using -1 has one advantage in a weakly typed language -- if you mess up and use the bitwise and operator instead of the logical and operator, your condition will still evaluate correctly as long as one of the operands has been converted to the canonical boolean representation. This isn't true if the canonical representation is 1.

使用-1在弱类型语言中有一个优点 - 如果你陷入困境并使用按位和运算符而不是逻辑和运算符,只要其中一个操作数已转换为规范布尔值,您的条件仍将正确评估表示。如果规范表示为1,则不是这样。

  0xffffffff & 0x00000010 == 0x00000010 (true)
  0xffffffff && 0x00000010 == 0xffffffff (true)

but

  0x00000001 & 0x00000010 == 0x00000000 (false)
  0x00000001 && 0x00000010 == 0xffffffff (true)

#5


IMO, if you want to stick with false=0x00, you should use 0x01. 0xFF is usually:

IMO,如果你想坚持假= 0x00,你应该使用0x01。 0xFF通常是:

  • a sign that some operation overflowed
  • 某些操作溢出的迹象

or

  • an error marker
  • 错误标记

And in both cases, it probably means false. Hence the *nix return value convention from executables, that true=0x00, and any non-zero value is false.

在这两种情况下,它可能意味着错误。因此,来自可执行文件的* nix返回值约定,即true = 0x00,并且任何非零值都是false。

#6


-1 is longer to type than 1...

-1比1更长的类型...

In the end it doesn't matter since 0 is false and anything else is true, and you will never compare to the exact representation of true.

最后它没关系,因为0是假的,其他任何事情都是真的,你永远不会与真实的确切表示进行比较。

Edit, for those down voting, please explain why. This answer is essentially the same as the one currently rated at +19. So that is 21 votes difference for what is the same basic answer.

编辑,对于那些投票,请解释原因。这个答案基本上与目前评级为+19的答案相同。所以这是相同的基本答案的21票差异。

If it is because of the -1 comment, it is true, the person who actually defines "true" (eg: the compiler writer) is going to have to use -1 instead of 1, assuming they chose to use an exact representation. -1 is going to take longer to type than 1, and the end result will be the same. The statement is silly, it was meant to be silly, because there is no real difference between the two (1 or -1).

如果是因为-1注释,那么实际定义“true”的人(例如:编译器编写者)将不得不使用-1而不是1,假设他们选择使用精确表示。 -1输入的时间比1要长,最终结果是相同的。声明很愚蠢,它本来是愚蠢的,因为两者之间没有真正的区别(1或-1)。

If you are going to mark something down at least provide a rationale for it.

如果你要标记某些东西至少提供一个理由。

#7


0xff is an odd choice since it has an implicit assumption that 8 bits is your minimum storage unit. But it's not that uncommon to want to store boolean values more compactly than that.

0xff是一个奇怪的选择,因为它隐含假设8位是你的最小存储单元。但是,想要比这更紧凑地存储布尔值并不常见。

Perhaps you want to rephrase by thinking about whether boolean operators produce something that is just one 0 or 1 bit (which works regardless of sign extension), or is all-zeroes or all-ones (and depends on sign extension of signed two's-complement quantities to maintain all-ones at any length).

也许你想通过考虑布尔运算符是否只产生一个0或1位(无论符号扩展是否有效),或者是全零或全1(并且取决于有符号二进制补码的符号扩展)来改写。在任何长度维持所有的数量)。

I think your life is simpler with 0 and 1.

我认为你的生活更简单,0和1。

#8


The pros are none, and the cons are none, too. As long as you provide an automatic conversion from integer to boolean, it will be arbitrary, so it really doesn't matter which numbers you choose.

专业人士都没有,缺点也没有。只要你提供从整数到布尔值的自动转换,它就是任意的,所以你选择哪个数字并不重要。

On the other hand, if you didn't allow this automatic conversion you'd have a pro: you wouldn't have some entirely arbitrary rule in your language. You wouldn't have (7 - 4 - 3) == false, or 3 * 4 + 17 == "Hello", or "Hi mom!" == Complex(7, -2).

另一方面,如果你不允许这种自动转换,你就有了专业人士:你的语言中没有完全任意的规则。你不会有(7 - 4 - 3)== false,或3 * 4 + 17 ==“你好”,或“嗨妈妈!” ==复杂(7,-2)。

#9


I think the C method is the way to go. 0 means false, anything else means true. If you go with another mapping for true, then you are left with the problem of having indeterminate values - that are neither true nor false.

我认为C方法是可行的方法。 0表示错误,其他任何表示都是真的。如果您使用另一个映射为true,那么您将面临具有不确定值的问题 - 既不是真也不是假。

If this is language that you'll be compiling for a specific instruction set that has special support for a particular representation, then I'd let that guide you. But absent any additional information, for an 'standard' internal representation, I'd go with -1 (all 1's in binary). This value extends well to whatever size boolean you want (single bit, 8-bit, 16, etc), and if you break up a "TRUE" or a "FALSE" into a smaller "TRUE" or "FALSE", its still the same. (where if you broke a 16 bit TRUE=0x0001 you'd get a FALSE=0x00 and a TRUE=0x01).

如果这是您将为特定表示特别支持的特定指令集编译的语言,那么我将指导您。但是如果没有任何附加信息,对于“标准”内部表示,我会使用-1(二进制中的所有1)。这个值很好地扩展到你想要的任何大小的布尔值(单位,8位,16等),如果你将“TRUE”或“FALSE”分解为更小的“TRUE”或“FALSE”,它仍然相同。 (如果你打破了16位TRUE = 0x0001,你会得到一个FALSE = 0x00和一个TRUE = 0x01)。

#10


Design the language so that 0 is false and non-zero is true. There is no need to "convert" anything, and thinking "non-zero" instead of some specific value will help you write the code properly.

设计语言,使0为假,非零为真。没有必要“转换”任何东西,并且认为“非零”而不是某些特定值将帮助您正确编写代码。

If you have built-in symbols like "True" then go ahead and pick a value, but always think "non-zero is true" instead of "0x01 is true".

如果你有像“True”这样的内置符号,那么继续选择一个值,但总是认为“非零为真”而不是“0x01为真”。

#11


Whatever you do, once you select your values don't change them. In FORTH-77, true and false were defined as 1 and 0. Then, FORTH-83 redefined them as -1 and 0. There were a not few (well ok, only a few, this is FORTH we are talking about) problems caused by this.

无论你做什么,一旦你选择了你的价值观,就不要改变它们。在FORTH-77中,true和false被定义为1和0.然后,FORTH-83将它们重新定义为-1和0.有一些(很好,只有少数,这是我们正在讨论的问题)问题由此引起的。