Why is “Yes” a value of -1 in MS Access database?

前端 未结 2 1584
自闭症患者
自闭症患者 2021-02-03 17:40

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

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 18:13

    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.

提交回复
热议问题