Numeral Systems
Most people just do calculations in the decimal numeral system. But there are also other numeral systems. The mostly used ones are binary, hexagonal and octal. In SpeQ you can specify values in these numeral systems by adding a prefix to a value. There are also functions available to get a value in a specific numeral system.
Overview
SpeQ handles the following numeral systems.
| Numeral System |
Base |
Prefix |
Symbols |
Example |
| Binary |
Base-2 |
0b |
01 |
0b1101
Ans = 13
Bin(13)
Ans = 0b1101
|
Decimal |
Base-10 |
(no prefix) |
0123456789 |
12
Ans = 12
Dec(0xF3)
Ans = 243
|
Hexadecimal |
Base-16 |
0x |
0123456789ABCDEF |
0xF3
Ans = 243
Hex(243)
Ans = 0xF3
|
Octal |
Base-8 |
0o |
01234567 |
0o23
Ans = 19
Oct(19)
Ans = 0o23
|
Important for the notation of numbers in other numeral systems is the system variable Bytes and the option Show leading zeros.
Binary numbers
Why should we bother about other numeral systems? Is the decimal system not enough? Well, for example computers do work with binary values. A computer can only store bits in it's memory. A bit can have a value 0 or 1, a binary value. You can write out a binary value as follows.
0101 = 0*23 + 1*22 + 0*21 + 1*20
1011 = 1*23 + 0*22 + 1*21 + 1*20
1111 = 1*23 + 1*22 + 1*21 + 1*20
etc...
|
With a binary number of four digits you can represent the decimal numbers 0 to 15.
| Decimal | Binary |
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
|
| Decimal | Binary |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 11 | 1011 |
| 12 | 1100 |
| 13 | 1101 |
| 14 | 1110 |
| 15 | 1111 |
|
To use binary values in SpeQ you have to add a prefix "0b" to the value, for example "0b1011".
Bytes and negative values
In computers bits are grouped in bytes: one byte contains 8 bits. With one byte you can represent 28 = 256 values. With two bytes you can represent 216 = 65536 values.
To be able to represent negative values the Two's complement rule is used. For example the value range for one byte is not chosen from 0 to 255, but -128 to 127. For a binary value of one byte (8 digits) the value representation is as follows:
| Decimal | Binary representation with 1 byte |
| 127 | 0111 1111 |
| 126 | 0111 1110 |
| ... | ... |
| 2 | 0000 0010 |
| 1 | 0000 0001 |
| 0 | 0000 0000 |
| -1 | 1111 1111 |
| -2 | 1111 1110 |
| ... | ... |
| -127 | 1000 0001 |
| -128 | 1000 0000 |
As you can see, the representation of the negative values depends on the number of bytes that are available for the value. For example with one byte the number 0b11111111 represents -1, but with two bytes it represents 255.
With SpeQ you can specify the number of bytes you want to work with to 1, 2 or 4. You can do this in the Settings window or from within the Workarea by adjusting the system variable Bytes. There is also an option to show or hide leading zeros in numbers, as described on the page Settings. For example if Bytes=1, there are 8 digits available for binary numbers. With this option checked, the answer of Bin(23) will be 0b00010111 instead of 0b10111.
Hexadecimal numbers
With the hexadecimal representation you can display numbers in a much shorter way than binary. The hexadecimal system is base-16 and has the symbols
| 0 1 2 3 4 5 6 7 8 9 A B C D E F |
where the value of A=10, B=11 C=12, D=13, E=14 and F=15. Each four digits in a binary number can be represented by one hexadecimal digit. The conversion is very easy. For example
| Binary |
1101 |
0011 |
0101 |
0001 |
| Hexadecimal |
D |
3 |
5 |
1 |
= D351 |
Hexadecimal numbers are for example used to represent twenty-four-bit colors in HTML files in the format #RRGGBB. Here RR specifies the value of the Red component of the color, GG the Green component and BB the Blue component. For example red=#FF0000, green=#008000 and blue=#0000FF.
To use hexadecimal values in SpeQ you have to add a prefix "0x" to the value, for example "0xD351".
octal numbers
The octal numeral system is base-8 and has the symbols
Each three digits in a binary number can be represented by one octal digit. For example
| Binary |
001 |
001 |
001 |
101 |
010 |
001 |
| Octal |
1 |
1 |
1 |
5 |
2 |
1 |
= 111521 |
To use octal values in SpeQ you have to add a prefix "0o" to the value, for example "0o111521".
Examples
'Examples of using different numeral systems
' A binary number
0b1101001101010001
Ans = 54097
' A hexagonal number
0xE3DD1
Ans = 933329
' An octal number
0o1773
Ans = 1019
' Specify the numeral system by enclosing
' the line with a function Bin, Dec, Hex or Oct
Bin(3 + 18 * 2)
Ans = 0b100111
' Convert a binary value to a hexadecimal value
Hex(0b1101001101010001)
Ans = 0xD351
Hex(26)
Ans = 0x1A
Hex(15)
Ans = 0xF
Hex(255)
Ans = 0xFF
Hex(65340)
Ans = 0xFF3C
0xFF3C
Ans = 65340
Hex(-12)
Ans = 0xFFFFFFF4
Oct(13)
Ans = 0o15
Oct(120 + 5)
Ans = 0o175
0o175
Ans = 125
' The specification of a the numeral system is lost when
' you perform an operation after the conversion.
Bin(24 + 2)
Ans = 0b11010
Bin(24) + 2
Ans = 26
' Negative values. First specify the number of bytes for the values
Bytes = 1;
0b11111111
Ans = -1
Bytes = 2;
0b11111111
Ans = 255
' Set Bytes to the maximum available
Bytes = 4;
See Also
Representation Functions,
System functions,
Wikipedia
|