The Haxial Programming Language
Literal Numbers
Integer Numbers

Integers are whole numbers, meaning numbers with no fractional portion. For example 12 is an integer but 12.5 (with a decimal point) is not.

To write an integer number in KL source code, simply write it as 12345 for example. The number cannot have spaces or commas (as thousands separators) in it.

Numbers in KL are by default in decimal (radix/base 10). Because computers are based on binary (radix 2), sometimes it is useful to be able to write a number in binary. To write a binary number, you must prefix it with "0b" (zero and "b") so the compiler knows it is a binary number (as opposed to a decimal number that just happens to contain only "0" and "1" digits).

Hexadecimal (radix 16) is also useful sometimes because 16 is a power of 2 (it is 2**4) and is thus directly compatible with binary (1 hexadecimal digit consists of exactly 4 binary digits). To write a hexadecimal number, prefix it with "0x". Being radix 16, it has more possible digit values than decimal. This means that "0" to "9" is insufficient for hexadecimal. We need 6 more symbols. We use the first 6 letters of the alphabet. Thus the 16 possible digit values in radix 16 are represented using these characters: 0123456789ABCDEF (lowercase a-f is also accepted.)

Here is the same number written 4 different ways:

1234   {decimal}
0x4D2  {hexadecimal}
0x4d2  {hexadecimal}
0b10011010010  {binary}

Internally all 3 forms are converted to binary by the compiler (computers are based on binary), so it does not matter which you use -- the result is exactly the same regardless of which you use. Being able to write the number in different radixes is only for your convenience, it does not affect the operation of the program.

Leading zero digits are insignificant, thus the following numbers are equivalent and have the same interpretation. In the case of hexadecimal and binary, the leading zero digits must appear after the prefix ("0x" or "0b").

1234      {decimal}
001234    {decimal}
0x4D2     {hexadecimal}
0x0004D2  {hexadecimal}
0b00010011010010  {binary}
00x4D2    {INVALID}

You might ask, if I cannot use space or comma as a thousands separator, is there any character that I can use as a thousands separator or to group digits in a number? Yes, you can use the underscore ("_") character. For example:

1_315_000    {decimal}
0xA1B2_4444  {hexadecimal}
0b11111111_00000000  {binary}

1_315_000 is interpreted the same as 1315000 (the underscores are simply skipped/ignored). In the case of decimal, the first digit cannot be an underscore. In the case of hexadecimal and binary, any underscores must be after the prefix.

Names in KL are not permitted to begin with a digit "0" to "9", but can contain "0" to "9" elsewhere within the name. This allows the compiler to easily and quickly distinguish between names and numbers. If it begins with "0" to "9", then it is interpreted as a number.

If a number begins with "-", then it is interpreted as a negative number. It is acceptable to prefix a number with "+" to explicitly specify a positive number, but the "+" can be omitted and positive is assumed by default. In the case of hexadecimal and binary, the "+" or "-" must appear before the "0x" or "0b" prefix. Here are some examples of valid positive and negative numbers:

-123     {decimal}
+123     {decimal}
-0xA1D2  {hexadecimal}
+0b111   {binary}
0b-111   {INVALID}

Some confusion may arise because the "+" and "-" characters are also used for addition and subtraction operators. These situations are distinguished by the presence of whitespace.

For example, "-3" is interpreted as the number negative 3, whereas "- 3" (with a space) is interpreted as subtract the positive number 3. Note that "9-1" (without space) is interpreted as 2 numbers, the number 9 followed by the number negative 1. If you wanted it to be interpreted as subtraction, then write it with spaces as "9 - 1". Likewise, "someVarName-46" is interpreted as a name followed by the number negative 46. If you wanted it to be interpreted as subtraction, then write it with spaces as "someVarName - 46".

Fractional Numbers

In situations where you are using floating-point maths (or perhaps also fixed-point maths), you can write a decimal number with a fractional portion as "123.456" (without quotes) for example. The "." character is used as the decimal point. "123" is the integer (whole number) portion, and "456" is the fractional portion.

When using a decimal point, there must be at least 1 digit ("0" to "9") on both sides of the decimal point. Extra leading and trailing zero digits can be included and are insigificant (do not change the interpretation).

The number cannot contain spaces or commas. Underscores can be included as a thousands separator (underscores are simply skipped/ignored). The first digit cannot be an underscore, and the characters immediately left and right of the decimal point cannot be an underscore.

The number can be prefixed with "+" or "-" to indicate positive or negative, with the same rules as for integers.

"Scientific notation" can be used. For example "1.2e3" or "1.2e+3" means 1200 (calculated as 1.2×103). "4.56e-5" means 0.0000456 (calculated as 4.56×10-5). The decimal point must be included, thus "1e8" is invalid (use "1.0e8" instead). The "e" must be lowercase. The exponent (the number after the "e") must be a positive or negative integer.

Here are some valid examples:

123.456
+123.456
-50.55
115_700.65
0023.300
0.0
0.5
42.0
1.2e3
4.56e-5

Here are some invalid examples:

42.
42._
.5
-.5
_.5
_102.73
1e3
2.0e
5.9e1.2
4.e-3
Text Character Numbers

So now you know how to write numbers in decimal, hexadecimal, and binary, but there is another representation. This one is slightly confusing to beginners. Firstly, realize that internally in a computer, everything is (and can only be) represented as a number. So if everything must be a number, how is the letter "A" represented inside a computer? As the number 65. And "B" is 66, and "C" is 67, and so forth. The uppercase and lowercase versions of the letter/character are represented using different numbers, so "A" is 65 while "a" is 97.

However it is difficult/inconvenient to remember which numbers are used for which letters of the alphabet (not to mention other characters such as "$"), so you can have the compiler automatically supply the number for a character by writing the character enclosed in single quotation marks (also known as apostrophes). Following you can see the same number written 4 different ways:

'A'   {character}
65    {decimal}
0x41  {hexadecimal}
0b01000001  {binary}

Note that there is a difference between the text characters '0' to '9' and the numbers 0 to 9. For example, there is a difference between the number 1 and the number for the text character '1'. There is a text character '1' and it is represented by the number 49. Perhaps it would have made more sense for the character '1' to be represented by the number 1 (or perhaps that would have been even more confusing).

Regardless of what makes the most sense, it has been established by convention that the text character '1' is represented as the number 49, and it is not worth the hassle of changing the standard even if it could be successfully argued that representing it using the number 1 makes more sense. For this reason, the following is the same number written 3 different ways:

'1'   {character}
49    {decimal}
0x31  {hexadecimal}

A quoted character sequence can also contain characters specified by name rather than literally. For a listing of character names, see the Literal/Quoted Text section of this documentation. Following you can see the same number written 4 different ways:

'<amp>'  {character by name}
'&'      {character literally}
0x26     {hexadecimal}
38       {decimal}

Do not confuse the effect of SINGLE quotation marks with the effect of DOUBLE quotation marks. Single quotation marks make a quoted CHARACTER sequence, whereas double quotation marks make a quoted TEXT sequence. Single quotation marks get the integer number of a character, whereas double quotation marks make a read-only text variable.

The following characters cannot appear literally/directly in a quoted character sequence, because they would make the source code confusing, ambiguous, unreliable, or disruptive to the compiler. If you want to get the number of one of these characters, you must specify it by name not literally.

NameNumberDescription
apos0x0027Apostrophe or single quotation mark.
quot0x0022Double quotation mark.
lt0x003CLess-than symbol.
gt0x003EGreater-than symbol.
lcbra0x007BLeft curly bracket/brace.
rcbra0x007DRight curly bracket/brace.
br0x000ALine break.
crtn0x000DCarriage return.
tab0x0009Horizontal tab.
0x0060Grave accent.

Following you can see some invalid quoted character sequences, and the correct sequence that was probably intended.

InvalidValid
''''<apos>'
'"''<quot>'
'<''<lt>'
'>''<gt>'
'{''<lcbra>'
'}''<rcbra>'

It is also possible to write a quoted character sequence containing multiple characters, for example as a 3 character code. In this case, the numbers of the characters are packed together as 8-bit values. There is a maximum of 8 characters (64 bits in total). Only basic characters can be used in a multiple-character sequence (because other characters require more than 8 bits each). The characters can be specified literally or by name. Following you can see the same number written 4 different ways:

'ABC'       {character}
0x414243    {hexadecimal}
0x41_42_43  {hexadecimal}
4276803     {decimal}

Another example, this time with 2 characters specified by name:

'<amp>GO<amp>'  {character}
0x26474F26      {hexadecimal}
642207526       {decimal}
Dec/Hex/Bin Number Listing

Following you can see how a number is written in decimal (radix 10), hexadecimal (radix 16), and binary (radix 2).

DecimalHexadec.Binary
00x00b0
10x10b1
20x20b10
30x30b11
40x40b100
50x50b101
60x60b110
70x70b111
80x80b1000
90x90b1001
100xA0b1010
110xB0b1011
120xC0b1100
130xD0b1101
140xE0b1110
150xF0b1111
160x100b10000
170x110b10001
180x120b10010
190x130b10011
200x140b10100
210x150b10101
220x160b10110
230x170b10111
240x180b11000
250x190b11001
260x1A0b11010
270x1B0b11011
280x1C0b11100
290x1D0b11101
300x1E0b11110
310x1F0b11111
320x200b100000
330x210b100001
340x220b100010
350x230b100011
360x240b100100
370x250b100101
380x260b100110
390x270b100111
400x280b101000
410x290b101001
420x2A0b101010
430x2B0b101011
440x2C0b101100
450x2D0b101101
460x2E0b101110
470x2F0b101111
480x300b110000
490x310b110001
500x320b110010

DecimalHexadecimalBinary
1000x640b1100100
1580x9E0b10011110
1590x9F0b10011111
1600xA00b10100000
1610xA10b10100001
2550xFF0b11111111
2560x1000b1_00000000
1_0000x3E80b11_11101000
1_0240x4000b100_00000000
4_0960x10000b10000_00000000
10_0000x27100b100111_00010000
65_5350xFFFF0b11111111_11111111
65_5360x1_00000b1_00000000_00000000
100_0000x1_86A00b1_10000110_10100000
1_000_0000xF_42400b1111_01000010_01000000
1_048_5760x10_00000b10000_00000000_00000000
10_000_0000x98_96800b10011000_10010110_10000000
16_777_2150xFF_FFFF0b11111111_11111111_11111111
16_777_2160x100_00000b1_00000000_00000000_00000000
100_000_0000x5F5_E1000b101_11110101_11100001_00000000
268_435_4560x1000_00000b10000_00000000_00000000_00000000
1_000_000_0000x3B9A_CA000b111011_10011010_11001010_00000000
4_294_967_2950xFFFF_FFFF0b11111111_11111111_11111111_11111111
4_294_967_2960x1_0000_00000b1_00000000_00000000_00000000_00000000