## Numeral SystemsMost 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.## OverviewSpeQ handles the following numeral systems.
Bytes and the option Show leading zeros.
## Binary numbersWhy 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 storebits 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.
## Bytes and negative valuesIn computers bits are grouped in bytes: one byte contains 8 bits. With one byte you can represent 2^{8} = 256 values. With two bytes you can represent 2^{16} = 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:
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 numbersWith the hexadecimal representation you can display numbers in a much shorter way than binary. The hexadecimal system is base-16 and has the symbols
To use hexadecimal values in SpeQ you have to add a prefix "0x" to the value, for example "0xD351". ## octal numbersThe octal numeral system is base-8 and has the symbols
## 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 AlsoRepresentation Functions, System functions, Wikipedia |