Why is "Yes" a value of -1 in MS Access database?
I'm looking at linked data in 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)
I imagine there must be a good reason, and I would like to know it.
Solution 1:
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
.
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.
Let us change the sign of 1111111111111111
. First invert; we get:
0000000000000000
Then add one:
0000000000000001
, this is 1
.
This is the proof that 1111111111111111
was the binary representation of -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
.