Skip to content

Numeral Systems

LawnMeower edited this page Jul 19, 2024 · 2 revisions

A numeral system is a method for expressing numbers that defines how numbers are visualized by their base. In computer science, the base of a number indicates how many different digits one place of a number can have.

Base

The base (also known as radix) of a numeral system defines the number of different values each digit can have. The most common one is base 10 (also known as the decimal system), where a digit can have a value between 0 and 9. In computer science, bases 2, 8, and 16 are commonly used and crucial to understand.

Binary

The binary system (also known as base 2 or the dual system) is the simplest numeral system. A digit in binary (also referred to as a bit) can be either 0 or 1. The value of a binary number is calculated by multiplying each digit by 2 raised to the power of its position index. The index starts at 0 from the right. The rightmost digit is the least significant bit, with an exponent of 0, meaning its value is z * 2⁰ (z can be either 0 or 1). Any other digit’s exponent is one more than the digit to its right. Binary numbers are noted with a subscript 2 (₂) in mathematical notation (e.g., 1010₂) or with a prefix b or 0b in programming (e.g., b1010 or 0b1010).

To convert a binary number to a decimal number, sum each digit multiplied by 2 raised to its exponent. For example, the binary number 1010₂ converts to decimal as follows: 0b1010 = 1 * 2³ + 0 * 2² + 1 * 2¹ + 0 * 2⁰ = 8 + 0 + 2 + 0 = 10.

To convert a decimal number to binary, divide the number by 2, recording a 1 for a remainder and 0 for no remainder, then write the remainders in reverse order. For example, converting 10 to binary: 10 / 2 = 5, remainder 0 5 / 2 = 2, remainder 1 2 / 2 = 1, remainder 0 1 / 2 = 0, remainder 1

Result: 0b1010

Octal

The octal system (also known as base 8) is less commonly used than the binary system. Octal numbers are noted with a subscript 8 (₈) in mathematical notation or a leading 0 in programming. They are often used to express file or memory region permissions, represented as read (r), write (w), or execute (e). In binary, these permissions can be represented as a three-digit number, where the least significant bit represents read, the middle bit write, and the most significant bit execute. Each octal digit can represent one of eight different configurations, corresponding to values between 0 and 7.

Below is a table of all combinations:

Permission Binary Octal
no access 0b000 00
r 0b001 01
w 0b010 02
rw 0b011 03
e 0b100 04
re 0b101 05
we 0b110 06
rwe 0b111 07

Hexadecimal

The hexadecimal system (also known as base 16 or just hex) is widely used in computer science. Hexadecimal numbers use additional characters to represent values between 10 and 15, using the letters A to F. Therefore, the hexadecimal digit A equals the decimal number 10, and F equals 15. Hexadecimal numbers are noted with a subscript 16 (₁₆) or an h in mathematical notation, or a leading 0x in programming (e.g., 0x8E3A). Each hexadecimal digit represents four binary bits, making it practical for representing binary data.

To convert a hexadecimal number to a decimal number, sum each digit's decimal value multiplied by 16 raised to the power of its position (rightmost digit is index 0). For example, the hexadecimal number 0x8E3A converts to decimal as follows: 0x8E3A = 8 * 16³ + 14 * 16² + 3 * 16¹ + 10 * 16⁰ = 32768 + 3584 + 48 + 10 = 36410.

To convert a decimal number to hexadecimal, divide the number by 16 and use the remainder to determine the hex digit. Continue dividing the quotient by 16 until the quotient is 0, then convert each remainder to its hex equivalent and write them in reverse order. For example, converting 1337 to hex: 1337 / 16 = 83.5625 | 0.5625 * 16 = 9 83 / 16 = 5.1875 | 0.1875 * 16 = 3 5 / 16 = 0.3125 | 0.3125 * 16 = 5

Result: 0x539

The table below lists the first 16 hexadecimal values alongside their representation in decimal, octal and binary. In the beginning this is a handy lookup.

Hexadecimal Decimal Octal Binary
0x0 0 00 0b0000
0x1 1 01 0b0001
0x2 2 02 0b0010
0x3 3 03 0b0011
0x4 4 04 0b0100
0x5 5 05 0b0101
0x6 6 06 0b0110
0x7 7 07 0b0111
0x8 8 010 0b1000
0x9 9 011 0b1001
0xA 10 012 0b1010
0xB 11 013 0b1011
0xC 12 014 0b1100
0xD 13 015 0b1101
0xE 14 016 0b1110
0xF 15 017 0b1111

Clone this wiki locally