User`s manual
mikroBASIC
- Basic Compiler for Microchip PIC microcontrollers
In order to fully understand this, we should recollect the data types.
Data type determines not only the range of values variable can hold, but also the
amount of RAM space it consumes. This is fundamental in practical programming.
Let's assume that our destination variable C is a byte, consuming 8 bits of PIC
RAM, spanning values 0 to 255. Now observe what really happens inside the PIC:
the result should be 369, but in binary representation it equals (1)01110001.
Because C is limited to 8 bits it will store the lower 8 bits while dropping the rest
of the information (the most significant bit). 01110001 equals 113 in decimal rep-
resentation.
dim testA as byte
dim testB as byte
dim Cc as word
main:
testA = 250
testB = 10
Cc = testA + testB
' this will always be correct because
' range for Cc is 0..65535 and maximum result
' of adding two bytes is only 255 + 255 = 510
end.
MikroElektronika:
Development
tools
-
Books
-
Compilers
mikroBASIC
making it simple...
67
page
=
+
+
1 01 11 100
0 00 0 00 01
1 1 1 10000
=
Lets see what happens
when we add two bytes
and assign result to
byte
First byte has value
241 and its binary
representation is
11110001
Second byte has value
128 and its binary
representation is
10000000
1
Result has 9 bits.
Because destination
byte can hold only 8
bits, the most
significant bit is lost
As byte3 holds
01110001, result is 113
instead of 369. Most
significant bit is lost:
(1)01110001
byte3
byte2 byte1