Learning to Program - Numeric Types


1. How do I make programs show numbers using different formats?

The Str function converts numbers to strings. This function allows field widths, left and right justification and display of real numbers in exponential format. If leading zeroes are required, Str can be used with a fieldwidth of one to ensure there are no leading or trailing spaces, then the resulting string can have leading "0" chars attached to the start of the string.

2. How do I get the Hi/Lo bytes or words from integers?

For most programming purposes this is not required, since it involves splitting integers into two words or four bytes, and a knowledge of binary becomes important. If you need to do this, first bear in mind that integers are stored in 4 bytes (-2,147,483,648 to +2,147,483,647), words are stored in two bytes (0 to 65,535) and a single byte stores eight binary bits (0 to 255). Thus an integer consists of two words (a high and a low word) and words consist of two bytes (a high and a low byte). Words and bytes are obtained as follows:

Description Function name Details
High Byte from a Word Hibyte Get the high byte from a word value. For example Hibyte(0xFF77) returns 0xFF.
High Word from an Integer Hiword Get the high word from an integer value. For example Hiword(0x7FFF3333) returns 0x7FFF.
Low Byte from a Word Lobyte Get the low byte from a word value. For example Lobyte(0xFF77) returns 0x77.
Low Word from an Integer Loword Get the low word from an integer value. For example Loword(0x7FFF3333) returns 0x3333.

To join two bytes to make a word value, the high byte is shifted to the left by 8 bits (equivalent to multiplying by 0x100 or 256), and the low byte is then added. The following code can be used:

  WordValue <- (byteHi * 256) + byteLo

To join two words to make an integer value, the high word is shifted to the left by 16 bits (equivalent to multiplying by 0x10000 or 65536), and the low word is then added. The following code can be used:

  IntegerValue <- (wordHi * 65536) + wordLo

Bear in mind that Ubercode uses signed integers. Therefore integers between 0x00000000 and 0x7FFFFFFF are greater than or equal to zero (0 to +2,147,483,647), and integers between 0x80000000 and 0xFFFFFFFF are negative values (-2,147,483,648 to -1).