Question about "not"

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about "not"

nandona
Why does not of "0" become "-1" and not 1?
Reply | Threaded
Open this post in threaded view
|

Re: Question about "not"

WBahn
Administrator
Uh... because that is how the VM language defines true and false.

Read Section 7.2.2 again.

There are distinct advantages to this representation and a number of languages over the years have used it.
Reply | Threaded
Open this post in threaded view
|

Re: Question about "not"

ashort
In reply to this post by nandona
think of it as binary digits.

not 0000 0000 0000 0000 = 1111 1111 1111 1111

not 1111 1111 1111 1111 = 0000 0000 0000 0000

Therefore you want "true" to be stored as 1111 1111 1111 1111
and you want "false" to be stored as 0000 0000 0000 0000
so that when you not true, you get false, and when you not false, you get true

1111 1111 1111 1111 in 2's complement happens to be -1, but it's easier in this case to think of it as all 1's and don't confuse yourself.

Reply | Threaded
Open this post in threaded view
|

Re: Question about "not"

WBahn
Administrator
While these are some of the advantages of having 'true' be represented by -1, this is not the reason that true is represented by -1 (though it is almost certainly what motivated the authors to specify this behavior).

The reason is that the VM language specifies that true will be represented by -1. The language could have just as easily specified that it be represented by 1 and, indeed, many languages (include C) do just that.

We need to get in the habit of first and foremost looking at the specified behavior of the language we are working with, especially when writing a translator or compiler for that language.