48
Programming with TIDE
©2000-2008 Tibbo Technology Inc.
Conversion of Enumeration Types
User-defined enumeration types are held in various variable types, depending on
the values associated with the constants within the enumeration type. This is
described in detail under
. Thus, they are converted
according to the variable type used to store them (described above).
4.2.4.3
Type conversion in expressions
we already explained what happens when you assign
the value of a variable of one type to a variable of another type. This section deals
the cases where variables of different types are used in expression. For example, if
x is byte and i is integer, what will happen when you do "x+i"?
The Virtual Machine operates on 16-bit values by default
Native data width for the Virtual Machine is 16 bits. When you are performing
calculations on 8-bit and/or 16-bit variables, result is always truncated to 16 bits.
Also, all intermediary calculations are done using 16-bit arithmetic. Consider the
following example first:
dim
x,y,z
as
byte
x=150
y=200
z=(x+y)/10
' result of 35 is within byte variable's range, but
intermidiary calculations require 16-bit ariphmetic.
The above example will give correct result. Even though all three variables are of
byte type, internal calculations are 16-bit, so when x+y produces 350, which is
beyond the range of the byte variable, the Virtual Machine handles this in the right
way. Now, let's see another example:
dim
i,j,k
as
word
i=50000
j=60000
k=(i+j)/10
' result will be 4446, which is incorrect. 32-bit ariphmetic
was not automatically used!
This example requires 32-bit calculations because i+j will produce a number which
is outside the scope of 16-bit calculations. However, our compiler will not invoke
32-bit calculations automatically. Read on, this is not all...
Mixing in a 32-variable will cause the compiler to use 32-bit calculations
For the example above, turn i or j into a dword. Now, your calculations will be
correct. Mixing in any 32-bit variable will aotumatically upgrade calculations to 32
bits!
55
45