The True True
Posted
#1
(In Topic #232)
Regular

Code
Print CByte(True)
Print CByte(False)
Print
Print CByte(Not True)
Print CByte(Not False)
Print
Print CByte(Not Not True)
Print CByte(Not Not False)
Dim b As Byte
b = 1 ' C's "True"
Print
Print CByte(b)
Print CByte(Not b)
Print CByte(Not Not b)
Your output should look like this:
255
0
0
255
255
0
1
254
1
What is going on? You have to look at the binary representations of a byte:
0000 0000 = 0
0000 0001 = 1
1111 1110 = 254 (unsigned interpretation)
1111 1110 = -2 (signed interpretation)
1111 1111 = 255 (unsigned interpretation)
1111 1111 = -1 (signed interpretation)
Try the above code with CInt instead of CByte. In the conversion to 4 bytes, the left most bit gets replicated upward. This is called sign extension.
This is why BASIC can use "and", "not", "or", "xor" for both logical and bitwise operations. Because -1 is the "True True".
C uses 1 for TRUE so it needs & and &&, ~ and ~~, | and ||, ^ and ^^ to accomplish the same thing.
And everybody has to learn the distinction between bitwise and logical.
Can this be a problem? You bet, an early version of the SQL drivers failed horribly because of this.
I was very pleased to see that Gambas is true to the BASIC standard, it has the true true. Another good design decision.
.... and carry a big stick!
Posted
Regular

In the "Characteristics" section:
* Boolean constant True has numeric value −1.[21] This is because the Boolean data type is stored as a two's complement signed integer. In this construct −1 evaluates to all-1s in binary (the Boolean value True), and 0 as all-0s (the Boolean value False). This is apparent when performing a (bitwise) Not operation on the two's complement value 0, which returns the two's complement value −1, in other words True = Not False. This inherent functionality becomes especially useful when performing logical operations on the individual bits of an integer such as And, Or, Xor and Not.[22] This definition of True is also consistent with BASIC since the early 1970s Microsoft BASIC implementation and is also related to the characteristics of CPU instructions at the time.
* Logical and bitwise operators are unified. This is unlike some C-derived languages (such as Perl), which have separate logical and bitwise operators. This again is a traditional feature of BASIC.
.... and carry a big stick!
1 guest and 0 members have just viewed this.



