Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
215
Explicit Conversion
Explicit conversion can be executed at any point by inserting type keyword (
byte,  word,  short,  integer, 
longint, 
or
  float
) ahead of the expression to be converted. The expression must be enclosed in parentheses. 
Explicit conversion can be performed only on the operand left of the assignment operator.
Special case is the conversion between signed and unsigned types. Explicit conversion between signed and unsigned 
data does not change binary representation of data — it merely allows copying of source to destination.
For example:
dim a as byte
dim b as short
‘...
b = -1
a = byte(b)  ‘ a is 255, not 1
‘ This is because binary representation remains
‘ 11111111; it’s just interpreted differently now
You can’t execute explicit conversion on the operand left of the assignment operator:
word(b) = a   ‘ Compiler will report an error
Conversions Examples
Here is an example of conversion:
 
program test
typedef TBytePtr as ^byte
dim arr as word[10]
    ptr as TBytePtr
dim a, b, cc as byte
dim dd as word
main:
  a = 241
  b = 128
  cc  = a + b         ‘ equals 113
  cc  = word(a + b)   ‘ equals 113
  dd  = a + b         ‘ equals 369
  ptr = TBytePtr(@arr)
  ptr = ^byte(@arr)
end.