Code: Select all
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)
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.