Binary and hexadecimal integer formats


Integers

Integers are positive or negative whole numbers, or zero. Integer values within the range from -9,223,372,036,854,775,808 (-263) to 9,223,372,036,854,775,807 (263 - 1) can be held in Analytica exactly, without any loss of precision. When an integer literal within this range is entered, it is stored as an integer value, and results of integer arithmetic operations result in integer results, as long as the operands do not involve non-integer values, or potentially non-integer values. Once an operation is encountered that involves a floating point number or would be expected to produce a floating point number, resulting values are stored as 64-bit floating point numbers, known as doubles, with an integer range from -9,007,199,254,740,992 (-252) to 9,007,199,254,740,992 (252) -- meaning that every integer within this range can be held exactly without a loss of precision.

Each atomic value in Analytica has a data type. There are four data types for numeric values: Floating point real (aka double), integer, fixed-point real and complex number. As long as you use integer operations on the integer datatype, you retain 64-bits of integer value (from -263 to 263 - 1). You can use the TypeOf function to examine the data type, which returns "Number" (for floating point real), "Integer", "FixedPointReal" or "ComplexNumber".

Decimal notation

We normally write integers in base 10, or decimal notation, using the digits from 0 to 9. When you select the Integer number format, integers are shown to full precision without resorting to exponential notation, as long as the underlying value is an integer data type. Non-integer values are shown in exponential format once they have a magnitude of 1016 or larger.

When coercing a number to text, specify "Integer" for the «format» parameter when using the NumberToText function to get this same base-10 notation at full precision for integer values.

Hexadecimal notation

Integers are sometimes written or entered in base 16, known as hexadecimal or just "hex". Hex uses the standard digits 0 thru 9 plus letters A thru F. When hex notation is used to enter or display an integer value in Analytica, it is always preceded with 0x, as in these examples:

0x25 = 37
0xa = 10
0x100 = 256
0xabcd9876 = 9007199254740992

You can set numbers to display in hex notation by selecting Hexadecimal from the Number Format Dialog.

You can also coerce numbers to Hex in text, thus:

NumberToText(34567, "Hex") → "0x8707"

When you type hex values up to 0x7fffffffffffffff, the value is an integer at full precision. Above that, the positive values are greater than 263 and are stored as floating point reals with a possible loss in precision. To type a negative value, simply place a minus-sign in front, e.g.,

-0xabc → -2748

However, when negative numbers are displayed in hex notation, they display in hex using two's complement, where the most significant bit is set, and hence it will have 16 hex digits.

NumberToText(-1, "Hex") → 0xffffffffffffffff
NumberToText(-2^62, "Hex") → 0xc000000000000000

Binary notation

In binary notation, base 2, integers are written using only 0s and 1s. In Analytica, numbers in binary notation are always preceded with 0b, as in these examples.

0b101 → 7
0b11111111 → 255
NumberToText(34567, "Binary") → "0b1000011100000111"

An attempt to enter more that 64 digits following the 0b results in a syntax error.

Negative numbers display in their 64-bit two's complement form.

NumberToText(-1, "Binary") → 0b1111111111111111111111111111111111111111111111111111111111111111

Numbers entered in binary notation having 64 bits with the first bit 1 are interpreted as negative numbers. It is usually much simpler, however, to type a minus sign in front.

See Also

Comments


You are not allowed to post comments.