Multiply Middle '*/'
Syntax
Assignment Variable = Variable */ Variable
Overview
Multiplies unsigned variables and/or constants, returning the middle 16 bits of the 32-bit result.
Operands
Variable can be a constant, variable or expression. Assignment Variable can be any valid variable type.
The Multiply Middle operator has the effect of multiplying a value by a whole number and a fraction. The whole number is the upper byte of the multiplier (0 to 255 whole units) and the fraction is the lower byte of the multiplier (0 to 255 units of 1 / 256 each).
Suppose we are required to multiply a value by 1.5. The whole number, and therefore the upper byte of the multiplier, would be 1, and the lower byte (fractional part) would be 128, since 128 / 256 = 0.5. It may be clearer to express the */ multiplier in Hex as $0180, since hex keeps the contents of the upper and lower bytes separate. Here's an example: -
Example
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim wMyVar1 as Word = 100 ' Create a variable and assign a value to it
wMyVar1 = wMyVar1 */ $0180 ' Multiply by 1.5 [1 + (128/256)]
HRsoutLn Dec wMyVar1 ' Display result (150).
To calculate constants for use with the */ instruction, put the whole number portion in the upper byte, then use the following formula for the value of the lower byte: -
int(fraction * 256)
For example, take the value of PI (3.14159). The upper byte would be $03 (the whole number), and the lower would be int(0.14159 * 256) = 36 ($24). So the constant PI for use with */ would be $0324. This isn't a perfect match for PI, but the error is only about 0.1%.
Note.
This operator enables compatibility with BASIC Stamp code, and melab's compiler code, but is rather obsolete considering the signed and unsigned 32-bit capabilities of the Positron8 compiler.
