Re: Why is "False" = "-1"?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jim Mack (jmack_at_mdxi.nospam.com)
Date: 07/26/04


Date: Mon, 26 Jul 2004 12:35:05 -0400

UncleWobbly wrote:
> "Jonathan Wood" wrote:
>> Uncle,
>>
>>> True = -1 because it is NOT 0 - here NOT is a logic function
>>>
>>> the logic function NOT inverts all the bits in the value and any
>>> non-zero result can be taken as true. But in the strictest sense,
>>>
>>> false=0000000000000000
>>>
>>> which when inverted is
>>>
>>> 1111111111111111 which is -1 in binary math (2's compliment)
>>
>> I don't know why people insist on saying this. Not is bitwise, not
>> logical. If it were logical, then NOT 1 would result in False (0).
>>
>> --
>
> before there were CPUs, there was logic. NOT 1 = 0, it doesn't
> matter how many bits.

Of course it matters. It would only not matter if you were running a
single-bit CPU. And besides, it matters because we ARE talking about
CPUs, and a particular language implementation. Your own example shows
that you know this.

Logical operators require that all inputs are booleans, and they
generate boolean states as a result. If the inputs are not already
boolean types, they're implicitly coerced, using the rule that 0 = false
and anything else = true.

But unlike C, classic VB has no logical operators, only bitwise
operators and boolean data types. It does not coerce numerics to
booleans when used as arguments to the bitwise operators, so that step
is left to the programmer (CBool). You can safely use the bitwise
operators as logical operators only if you insure that the inputs are
booleans, or equivalent.

The Not operator inverts, individually, each of the bits in its
argument. Only when all the bits have the same value will Not perform a
logical negation, as a side-effect of its real purpose.

============

As a side note for anyone reading this thread: we can (but shouldn't)
rely on implicit conversion when assigning a value to a boolean -- for
example:

Dim Boole as Booean
  .....
Boole = 37 ' Does an implicit Boole = CBool(37)

But there's at least one situation where after an assignment you can end
up with an indeterminate value in a VB Boolean data type -- that is,
where not all bits have the same state. Does anyone know what it is?
(I'm not talking about CopyMem or LSet or other deliberate tricks.)
100 Nerd Points for the first answer.

-- 
        Jim


Relevant Pages

  • Re: Why is "False" = "-1"?
    ... Logical operators require that all inputs are booleans, ... generate boolean states as a result. ... But unlike C, classic VB has no logical operators, only bitwise ... operators and boolean data types. ...
    (microsoft.public.vb.general.discussion)
  • Re: Overloaded logic operators
    ... x and x.foo# if x is not nil, ... In languages such as Ruby, Lua, Io, Python and JavaScript, does the ... Let the default boolean type be a type with more than two values, ... includes sequential logical operators that are not bitwise). ...
    (comp.compilers)
  • Re: True = -1 ?
    ... logical refers to a boolean state. ... call the two types of operations bitwise and Boolean. ...
    (microsoft.public.vb.general.discussion)
  • Re: Boolean data type?
    ... What type is commonly used in C for playing around with boolean ... It might even make sense to store a boolean value in each bit ... (but you'll have to write your own bitwise masking and shifting code ... Assuming C99's _Bool is not available, ...
    (comp.lang.c)
  • Re: Form k = i + j and test for overflow.
    ... and declare it as anything _other_ than integer in Fortran, ... C itself has no boolean data type, but it has entities that require ... C programmers are encouraged to use any int as a boolean ... the logical operators do the right thing, ...
    (comp.lang.fortran)