User`s manual
mikroBASIC
- Basic Compiler for Microchip PIC microcontrollers
Special case is conversion between signed and unsigned. It is important to under-
stand that explicit conversion between signed and unsigned data does not change
binary representation of data; it merely allows copying of source to destination.
Example 1:
dim tA as byte
dim tB as byte
if tA + tB > tC then
tA = 0
end if
This could be wrong, because there is an expression on the left. Compiler evalu-
ates it, and treats it as a variable of type that matches type of tA or tB (the larger
of the two); in this case - a byte.
tA = 250
tB = 10
tC = 20
if tA + tB > tC then
tA = 0
end if
In this case, since the result of the expression is treated as byte, we get that 250 +
10 is lower than 20. Actually, the result of the expression is truncated to byte:
250 + 10 is 4, and 4 is lower than 20.
But if we wrote it like this:
if word(tA + tB) > tC then
tA = 0
end if
. . it would be correct, because we have explicitly instructed the compiler to treat
tA + tB as a word. Hence, the result will equal 260 and greater than 20, returning
the expected result.
MikroElektronika:
Development
tools
-
Books
-
Compilers
mikroBASIC
making it simple...
63
page