Any base, signed n-bit numbers, step by step, and text to bytes
Type in any box and the rest update instantly. Numbers of any size are accepted, as well as prefixes such as 0x or 0b and spaces between groups of bits.
Unicode character with that code:
Each character is stored as one or more bytes according to the UTF-8 encoding: ASCII characters (unaccented letters, digits and basic symbols) take one byte, while «ñ», accented vowels or «€» take two or three. Type in any box; in binary and decimal, separate the bytes with spaces.
In a positional system with base \(b\), each digit is worth its value multiplied by a power of the base according to its position, starting with \(b^0\) on the right. A number with digits \(d_{k}\dots d_1 d_0\) is worth:
The base tells you how many different digits there are: 10 in decimal (0–9), 2 in binary (0 and 1), 8 in octal (0–7) and 16 in hexadecimal (0–9 and A–F). The base is shown with a subscript (\(101_2\)) or a prefix: 0b101, 0o17, 0x1F (or 1Fh and 16#1F in PLCs).
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
It is the system of digital circuits: each digit (bit) is a 0 or a 1, which the hardware represents with two voltage levels. 4 bits make a nibble, 8 bits a byte and 16 or 32 bits a word (WORD, DWORD). The leftmost bit is the most significant (MSB) and the rightmost the least significant (LSB). With n bits you can count \(2^n\) values, from 0 to \(2^n - 1\).
They are compact ways of writing binary: since \(8 = 2^3\) and \(16 = 2^4\), each octal digit equals 3 bits and each hexadecimal digit 4. To convert, just group the bits from the right: \(1011\,0110_2 = \text{B6}_{16}\) and \(10\,110\,110_2 = 266_8\).
Hexadecimal is everywhere: memory addresses, web colours (#1F6FEB), MAC addresses, error codes and Modbus registers. Octal is used mainly in Unix permissions (chmod 755) and in some older PLCs.
\(45 = 0010\,1101\). Invert the bits: \(1101\,0010\). Add 1: \(1101\,0011 = \text{D3}_{16}\). Check: \(-128 + 64 + 16 + 2 + 1 = -45\).
| Value (4 bits) | Sign and magnitude | One's complement | Two's complement | Excess 8 |
|---|---|---|---|---|
| +7 | 0111 | 0111 | 0111 | 1111 |
| +1 | 0001 | 0001 | 0001 | 1001 |
| 0 | 0000 and 1000 | 0000 and 1111 | 0000 | 1000 |
| −1 | 1001 | 1110 | 1111 | 0111 |
| −7 | 1111 | 1000 | 1001 | 0001 |
| −8 | – | – | 1000 | 0000 |
| Range with n bits | \(\pm(2^{n-1}-1)\) | \(\pm(2^{n-1}-1)\) | \(-2^{n-1} \dots 2^{n-1}-1\) | \(-2^{n-1} \dots 2^{n-1}-1\) |
If the result of an operation falls outside the range there is an overflow: in 16 bits, 32767 + 1 gives −32768. It is a classic bug in PLC counters of type INT.
The ASCII code gives a 7-bit number to every letter, digit and symbol: «A» is 65 (0x41), «a» is 97 (0x61) and «0» is 48 (0x30), so a digit is turned into its value by subtracting 0x30. Codes 0 to 31 are control codes, such as line feed (LF, 0x0A), carriage return (CR, 0x0D) or the STX and ETX that mark the start and end of a frame in many serial devices.
UTF-8 extends ASCII to the whole of Unicode without changing those first 128 codes: the other characters use 2 to 4 bytes, and the first byte says how many follow. That is why a text with accents «takes up more» than it seems, and why odd characters such as «Ã±» appear instead of «ñ» when a device reads those bytes as Latin-1. Base64 is not a text encoding but a way of writing any bytes with 64 printable characters (4 characters for every 3 bytes); it is used in e-mail, certificates and web requests.
Related: Boolean algebra, Karnaugh map and PLC ladder simulator.