Skip to main content

Numeric

Numeric is a generic term that stands for:

  • Number attribute, variable or expression. The range for the number data type is ±1.7e±308 (13 significant digits).
  • Integer attribute, variable or expression. The range for the integer data type (4-byte Long integer) is -2^31..(2^31)-1.

You can assign any numeric data type to another; Qodly does the conversion, truncating or rounding if necessary. However, when values are out of range, the conversion will not return a valid value. You can mix numeric data types in expressions.

Numeric literals

A numeric literal constant is written as a real number with a period (.) as decimal separator. Here are some examples of numeric constants:

27
123.76
0.0076

Negative numbers are specified with the minus sign (-). For example:

-27
-123.76
-0.0076

Numeric operators

OperationSyntaxReturnsExpressionValue
AdditionNumber + NumberNumber2 + 35
SubtractionNumber - NumberNumber3 – 21
MultiplicationNumber * NumberNumber5 * 210
DivisionNumber / NumberNumber5 / 22.5
Longint divisionNumber \ NumberNumber5 \ 22
ModuloNumber % NumberNumber5 % 21
ExponentiationNumber ^ NumberNumber2 ^ 38
EqualityNumber == NumberBoolean10 == 10true
10 == 11false
InequalityNumber != NumberBoolean10 != 11true
10 != 10false
Greater thanNumber > NumberBoolean11 > 10true
10 > 11false
Less thanNumber < NumberBoolean10 < 11true
11 < 10false
Greater than or equal toNumber >= NumberBoolean11 >= 10true
10 >= 11false
Less than or equal toNumber <= NumberBoolean10 <= 11true
11 <= 10false

The modulo operator % divides the first number by the second number and returns a whole number remainder. Here are some examples:

  • 10 % 2 returns 0 because 10 is evenly divided by 2.
  • 10 % 3 returns 1 because the remainder is 1.
  • 10.5 % 2 returns 0 because the remainder is not a whole number.
caution
  • The modulo operator % returns significant values with numbers that are in the integer range (from minus 2^31 to 2^31 minus one). To calculate the modulo with numbers outside of this range, use the mod command.
  • The longint division operator \ returns significant values with integer numbers only.

Precedence

The order in which an expression is evaluated is called precedence. Qodly has a strict left-to-right precedence, in which algebraic order is not observed. For example:

 3+4*5

returns 35, because the expression is evaluated as 3 + 4, yielding 7, which is then multiplied by 5, with the final result of 35.

To override the left-to-right precedence, you MUST use parentheses. For example:

 3+(4*5)

returns 23 because the expression (4 * 5) is evaluated first, because of the parentheses. The result is 20, which is then added to 3 for the final result of 23.

Parentheses can be nested inside other sets of parentheses. Be sure that each left parenthesis has a matching right parenthesis to ensure proper evaluation of expressions. Lack of, or incorrect use of parentheses can cause unexpected results or invalid expressions. Furthermore, if you intend to compile your applications, you must have matching parentheses—the compiler detects a missing parenthesis as a syntax error.

Bitwise operators

The bitwise operators operates on integer expressions or values. If you pass a real value to a bitwise operator, Qodly evaluates the value as a integer value before calculating the expression that uses the bitwise operator.

While using the bitwise operators, you must think about an integer value as an array of 32 bits. The bits are numbered from 0 to 31, from right to left.

Because each bit can equal 0 or 1, you can also think about an integer value as a value where you can store 32 boolean values. A bit equal to 1 means true and a bit equal to 0 means false.

An expression that uses a bitwise operator returns an integer value, except for the Bit Test operator, where the expression returns a boolean value. The following table lists the bitwise operators and their syntax:

OperationOperatorSyntaxReturns
Bitwise AND&integer & integerinteger
Bitwise OR (inclusive)|integer | integerinteger
Bitwise OR (exclusive)^|integer ^| integerinteger
Left Bit Shift<<integer << integerinteger (see note 1)
Right Bit Shift>>integer >> integerinteger (see note 1)
Bit Set?+integer ?+ integerinteger (see note 2)
Bit Clear?-integer ?- integerinteger (see note 2)
Bit Test??integer ?? integerboolean (see note 2)

Notes

  1. For the Left Bit Shift and Right Bit Shift operations, the second operand indicates the number of positions by which the bits of the first operand will be shifted in the resulting value. Therefore, this second operand should be between 0 and 31. Note however, that shifting by 0 returns an unchanged value and shifting by more than 31 bits returns 0x00000000 because all the bits are lost. If you pass another value as second operand, the result is non-significant.
  2. For the Bit Set, Bit Clear and Bit Test operations , the second operand indicates the number of the bit on which to act. Therefore, this second operand must be between 0 and 31; otherwise, the result of the expression is non-significant.

The following table lists the bitwise operators and their effects:

OperationDescription
Bitwise ANDEach resulting bit is the logical AND of the bits in the two operands. Here is the logical AND table:
  • 1 & 1 --> 1
  • 0 & 1 --> 0
  • 1 & 0 --> 0
  • 0 & 0 --> 0
  • In other words, the resulting bit is 1 if the two operand bits are 1; otherwise the resulting bit is 0.
    Bitwise OR (inclusive)Each resulting bit is the logical OR of the bits in the two operands.Here is the logical OR table:
  • 1 | 1 --> 1
  • 0 | 1 --> 1
  • 1 | 0 --> 1
  • 0 | 0 --> 0
  • In other words, the resulting bit is 1 if at least one of the two operand bits is 1; otherwise the resulting bit is 0.
    Bitwise OR (exclusive)Each resulting bit is the logical XOR of the bits in the two operands.Here is the logical XOR table:
  • 1 \^| 1 --> 0
  • 0 \^| 1 --> 1
  • 1 \^| 0 --> 1
  • 0 \^| 0 --> 0
  • In other words, the resulting bit is 1 if only one of the two operand bits is 1; otherwise the resulting bit is 0.
    Left Bit ShiftThe resulting value is set to the first operand value, then the resulting bits are shifted to the left by the number of positions indicated by the second operand. The bits on the left are lost and the new bits on the right are set to 0. Note: Taking into account only positive values, shifting to the left by N bits is the same as multiplying by 2^N.
    Right Bit ShiftThe resulting value is set to the first operand value, then the resulting bits are shifted to the right by the number of position indicated by the second operand. The bits on the right are lost and the new bits on the left are set to 0.Note: Taking into account only positive values, shifting to the right by N bits is the same as dividing by 2^N.
    Bit SetThe resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 1. The other bits are left unchanged.
    Bit ClearThe resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 0. The other bits are left unchanged.
    Bit TestReturns true if, in the first operand, the bit whose number is indicated by the second operand is equal to 1. Returns false if, in the first operand, the bit whose number is indicated by the second operand is equal to 0.

    Examples

    OperationExampleResult
    Bitwise AND0x0000FFFF & 0xFF00FF000x0000FF00
    Bitwise OR (inclusive)0x0000FFFF | 0xFF00FF000xFF00FFFF
    Bitwise OR (exclusive)0x0000FFFF \^| 0xFF00FF000xFF0000FF
    Left Bit Shift0x0000FFFF << 80x00FFFF00
    Right Bit Shift0x0000FFFF >> 80x000000FF
    Bit Set0x00000000 ?+ 160x00010000
    Bit Clear0x00010000 ?- 160x00000000
    Bit Test0x00010000 ?? 16True