Re: Why is "False" = "-1"?
From: Jim Mack (jmack_at_mdxi.nospam.com)
Date: 07/26/04
- Next message: Fie Fie Niles: "Re: Can LDAP API be used either from VB or VBScript ?"
- Previous message: Atreju: "Please help my mess of code"
- In reply to: UncleWobbly: "Re: Why is "False" = "-1"?"
- Next in thread: Jonathan Wood: "Re: Why is "False" = "-1"?"
- Reply: Jonathan Wood: "Re: Why is "False" = "-1"?"
- Reply: Jim Carlock: "Re: Why is "False" = "-1"?"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Fie Fie Niles: "Re: Can LDAP API be used either from VB or VBScript ?"
- Previous message: Atreju: "Please help my mess of code"
- In reply to: UncleWobbly: "Re: Why is "False" = "-1"?"
- Next in thread: Jonathan Wood: "Re: Why is "False" = "-1"?"
- Reply: Jonathan Wood: "Re: Why is "False" = "-1"?"
- Reply: Jim Carlock: "Re: Why is "False" = "-1"?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|