SQL 为什么 MS Access 数据库中“是”的值为 -1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8827447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is "Yes" a value of -1 in MS Access database?
提问by supermitch
I'm looking at linked data in MS Access.
我正在查看 MS Access 中的链接数据。
The "Yes/No" fields contain the value -1 for YES and 0 for NO. Can someone explain why such a counter-intuitive value is used for "Yes"? (Obviously, it should be 1 and 0)
“是/否”字段包含值 -1 表示 YES 和 0 表示否。有人可以解释为什么这样一个违反直觉的值用于“是”?(显然,它应该是 1 和 0)
I imagine there must be a good reason, and I would like to know it.
我想一定有一个很好的理由,我很想知道。
回答by Olivier Jacot-Descombes
The binary representation of False
is 0000000000000000
(how many bits are used depends on the implementation). If you perform a binary NOT operation on it, it will be changed to 1111111111111111
, i.e. True
, but this is the binary representation of the signed integer -1
.
的二进制表示False
是0000000000000000
(使用多少位取决于实现)。如果对其执行二进制 NOT 运算,它将更改为1111111111111111
,即True
,但这是有符号整数的二进制表示-1
。
A bit of 1
at the most significant position signals a negative number for signed numbers. Changing the sign of a number happens by inverting all the bits and adding 1. This is called the Two's complement.
的位1
在最显著位置信号的符号数为负数。通过反转所有位并加 1 来更改数字的符号。这称为二进制补码。
Let us change the sign of 1111111111111111
. First invert; we get:
0000000000000000
让我们改变 的符号1111111111111111
。第一次反转;我们得到:
0000000000000000
Then add one:
0000000000000001
, this is 1
.
然后加一个:
0000000000000001
,这是1
。
This is the proof that 1111111111111111
was the binary representation of -1
.
这是 的1111111111111111
二进制表示的证明-1
。
UPDATE
更新
Also, when comparing these values do not compare
此外,在比较这些值时,不要比较
x = -1
or
或者
x = 1
instead, do compare
相反,做比较
x <> 0
this always gives the correct result, independently of the convention used. Most implementations treat any value unequal zero as True
.
这总是给出正确的结果,与使用的约定无关。大多数实现将任何不等于零的值视为True
。
回答by cdeszaq
"Yes" is -1 because it isn't anything else.
“是”是 -1,因为它不是其他任何东西。
When dealing with Microsoft products, especially one as old as Access, don't assume that there is a good reason for anydesign choice.
在处理 Microsoft 产品时,尤其是像 Access 一样古老的产品时,不要假设任何设计选择都有充分的理由。