Binary and hexadecimal number systems

  • Computer Science
  • binary number system
  • number system conversions
  • two's complement
  • decimal to binary
  • binary to hexadecimal
  • positional number systems

Podcast

Poziční Číselné Soustavy: Od Bitů k Dvojkovému Doplňku0:00 / 12:19
0:001:00 remaining

Understanding how computers speak is fundamental to grasping the digital world. At the core of this language are binary and hexadecimal number systems, alongside other positional systems like octal and the familiar decimal. This article will break down these essential concepts, explore how to convert between them, and even touch upon how computers handle negative numbers using two's complement.

Demystifying Binary and Hexadecimal Number Systems

Positional number systems are the backbone of how numbers are represented and manipulated, particularly in digital electronics and computer science. Unlike non-positional systems, the value of each digit depends on both the digit itself and its position within the number. While humans commonly use the decimal system, computers heavily rely on binary, octal, and hexadecimal systems.

What are Positional Systems?

Every positional number system is defined by its base, also known as its radix. This base dictates the number of unique digits or symbols used. For instance, the decimal system, with a base of 10, uses ten digits (0-9).

The value of a number in any positional system can be expressed as a sum of products. Each product consists of a digit multiplied by the base raised to the power of its position. The position is counted from right to left, starting with 0 for the rightmost (least significant) digit.

For a number represented as d_n d_{n-1}... d_1 d_0. d_{-1} d_{-2}... d_{-m} in base B, its value is:

Value = d_n · B^n + d_{n-1} · B^{n-1} +... + d_1 · B^1 + d_0 · B^0 + d_{-1} · B^{-1} + d_{-2} · B^{-2} +... + d_{-m} · B^{-m}

The Decimal System (Base 10)

The decimal system is the most familiar positional number system. It has a base of 10 and uses ten distinct digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Each position in a decimal number represents a power of 10.

Example: The number 345.25 in decimal can be broken down as:

3 · 10^2 + 4 · 10^1 + 5 · 10^0 + 2 · 10^{-1} + 5 · 10^{-2} = 3 · 100 + 4 · 10 + 5 · 1 + 2 · 0.1 + 5 · 0.01 = 300 + 40 + 5 + 0.2 + 0.05 = 345.25

The Binary System (Base 2)

The binary system is often considered the native language of computers. It has a base of 2 and uses only two digits: 0 and 1, known as bits (binary digits). Computers use binary because electronic circuits can easily represent these two states (on/off, high/low voltage).

Each position in a binary number represents a power of 2.

Example: Convert 1101_2 to decimal

1 · 2^3 + 1 · 2^2 + 0 · 2^1 + 1 · 2^0 = 1 · 8 + 1 · 4 + 0 · 2 + 1 · 1 = 8 + 4 + 0 + 1 = 13_{10}

The Octal System (Base 8)

The octal system has a base of 8 and uses eight digits: 0 through 7. Historically, it was used in some early computer systems to represent binary numbers more compactly. Three binary digits (bits) can represent one octal digit (2^3 = 8).

While less common than hexadecimal today, octal is still used in specific contexts, such as file permissions in Unix-like operating systems.

Example: Convert 27_8 to decimal

2 · 8^1 + 7 · 8^0 = 2 · 8 + 7 · 1 = 16 + 7 = 23_{10}

The Hexadecimal System (Base 16)

The hexadecimal system, with its base of 16, is widely used in computing. It employs sixteen symbols: 0-9 and A-F. The letters A through F represent decimal values 10 through 15, respectively.

Hexadecimal offers a concise way to represent large binary numbers, as four binary digits can represent one hexadecimal digit (2^4 = 16). This makes it ideal for representing memory addresses, color codes (e.g., #FF0000), and other data in a human-readable format.

Example: Convert 1A_16 to decimal

1 · 16^1 + A · 16^0 = 1 · 16 + 10 · 1 = 16 + 10 = 26_{10}

Check what you already know

Test · 20 questions · first 3 free

Question 1 of 30%

The range of numbers that can be represented by an n-bit two's complement system is from -2^(n-1) to 2^(n-1) - 1.

Practise it with flashcards

26 cards · first 3 free, no account

1 / 26

What defines a positional number system?

In a positional system, the value of each digit in a number depends not only on the digit itself but also on its position within the number.

Tap to flip · Swipe to navigate

Essential Number System Conversions

Converting numbers between different bases is a critical skill in computer science. Here are the common methods for number system conversions.

1. Converting from Any Base to Decimal

To convert a number from any base B to its decimal equivalent, use the positional expansion formula:

Value = Σ (d_i · B^i), where d_i is the digit at position i, and i is the position exponent.

Example: Convert 11010_2 to decimal

1 · 2^4 + 1 · 2^3 + 0 · 2^2 + 1 · 2^1 + 0 · 2^0 = 1 · 16 + 1 · 8 + 0 · 4 + 1 · 2 + 0 · 1 = 16 + 8 + 0 + 2 + 0 = 26_{10}

Example: Convert 37_8 to decimal

3 · 8^1 + 7 · 8^0 = 3 · 8 + 7 · 1 = 24 + 7 = 31_{10}

Example: Convert C5_16 to decimal

C · 16^1 + 5 · 16^0 = 12 · 16 + 5 · 1 = 192 + 5 = 197_{10}

2. Converting from Decimal to Any Base

To convert a decimal number to another base B, use the method of repeated division by the target base. Follow these steps:

  1. Divide the decimal number by the target base and record the remainder.
  2. Divide the quotient from the previous step by the base again.
  3. Continue this process until the quotient becomes 0.
  4. The new number in base B is formed by reading the remainders from bottom to top (the last remainder is the most significant digit).

Example: Convert 26_{10} to binary

  • 26 ÷ 2 = 13 remainder 0
  • 13 ÷ 2 = 6 remainder 1
  • 6 ÷ 2 = 3 remainder 0
  • 3 ÷ 2 = 1 remainder 1
  • 1 ÷ 2 = 0 remainder 1

Reading remainders from bottom to top: 11010_2

Example: Convert 31_{10} to octal

  • 31 ÷ 8 = 3 remainder 7
  • 3 ÷ 8 = 0 remainder 3

Reading remainders from bottom to top: 37_8

Example: Convert 197_{10} to hexadecimal

  • 197 ÷ 16 = 12 remainder 5
  • 12 ÷ 16 = 0 remainder 12 (which is C in hexadecimal)

Reading remainders from bottom to top: C5_16

3. Converting Between Binary, Octal, and Hexadecimal

These conversions are particularly straightforward because their bases are powers of 2 (2^1, 2^3, 2^4). This relationship allows for direct grouping or expansion of bits.

Binary to Octal Conversion

Group binary digits into sets of three, starting from the right. If the leftmost group has fewer than three bits, pad it with leading zeros. Convert each group of three bits into its corresponding octal digit.

Example: Convert 11010110_2 to octal

  • Group: 011 010 110 (padded with a leading zero)
  • Convert: 3 2 6
  • Result: 326_8

Octal to Binary Conversion

Convert each octal digit into its three-bit binary equivalent.

Example: Convert 57_8 to binary

  • Convert 5: 101
  • Convert 7: 111
  • Result: 101111_2

Binary to Hexadecimal Conversion

Group binary digits into sets of four, starting from the right. Pad the leftmost group with leading zeros if it has fewer than four bits. Convert each group of four bits into its corresponding hexadecimal digit.

Example: Convert 11010110_2 to hexadecimal

  • Group: 1101 0110
  • Convert: D 6
  • Result: D6_16

Hexadecimal to Binary Conversion

Convert each hexadecimal digit into its four-bit binary equivalent.

Example: Convert A3_16 to binary

  • Convert A (10): 1010
  • Convert 3: 0011
  • Result: 10100011_2

Understanding Two's Complement

In computer systems, negative numbers are typically represented using two's complement. This system simplifies arithmetic operations for both positive and negative numbers, allowing them to be handled by the same hardware logic. Two's complement is used for signed integers, where the most significant bit (MSB) indicates the sign (0 for positive, 1 for negative).

How to Find Two's Complement

To find the two's complement of a negative number:

  1. Represent the absolute value of the number in binary. Ensure it has a fixed number of bits (e.g., 8-bit, 16-bit), which defines the range.
  2. Invert all the bits (perform a one's complement). Change all 0s to 1s and all 1s to 0s.
  3. Add 1 to the result.

Example: Find the 8-bit two's complement of -5_{10}

  1. Absolute value of 5 in 8-bit binary: 00000101_2
  2. Invert all bits (one's complement): 11111010_2
  3. Add 1: 11111010_2 + 1_2 = 11111011_2

So, -5_{10} in 8-bit two's complement is 11111011_2.

Converting Two's Complement Back to Decimal

To convert a two's complement negative number back to decimal:

  1. If the MSB is 1 (indicating a negative number), invert all bits.
  2. Add 1 to the inverted result.
  3. Convert the resulting binary number to decimal.
  4. Place a negative sign in front of the decimal value.

Example: Convert 11111011_2 (8-bit two's complement) to decimal

  1. MSB is 1, so it's negative. Invert bits: 00000100_2
  2. Add 1: 00000100_2 + 1_2 = 00000101_2
  3. Convert to decimal: 0 · 2^7 +... + 1 · 2^2 + 0 · 2^1 + 1 · 2^0 = 4 + 1 = 5_{10}
  4. Add negative sign: -5_{10}

The range of numbers that can be represented by an n-bit two's complement system is from -2^(n-1) to 2^(n-1) - 1. For an 8-bit system, this range is -128 to 127.

Common Mistakes and Misconceptions

When working with number systems, students often encounter specific challenges. Being aware of these can help in mastering the concepts.

  • Confusing place values: A frequent error is miscalculating the power of the base for a digit's position. Always remember the rightmost digit is at position 0.
  • Incorrectly applying conversion rules: Forgetting to read remainders from bottom to top in decimal-to-any-base conversions, or incorrect grouping in binary-octal/hexadecimal conversions, are common. Ensure consistent grouping from the right.
  • Forgetting the 'add 1' in two's complement: Simply inverting bits (one's complement) is insufficient; adding 1 is crucial for correct negative number representation.
  • Misunderstanding the range of two's complement: An n-bit system represents more negative numbers than positive ones. For an 8-bit system, it's -128 to 127, not -127 to 127.
  • Mixing up hexadecimal letters: Incorrectly assigning decimal values to hexadecimal letters (A=10, B=11, C=12, D=13, E=14, F=15) can lead to conversion errors.

Learning Through Interaction: Number System Simulations

Interactive simulations offer a dynamic and effective way to understand positional number systems. A typical simulation allows users to manipulate individual bits (0 or 1) in a binary representation. As a bit is switched, the simulation immediately updates the corresponding decimal, octal, and hexadecimal values.

This direct feedback reinforces the concept of place value: changing a bit in a higher position (further left) causes a significantly larger change in the overall numerical value than changing a bit in a lower position (further right). For example, in an 8-bit display, changing the rightmost bit (2^0) from 0 to 1 adds 1 to the decimal value. In contrast, changing the leftmost bit (2^7 for unsigned numbers) from 0 to 1 adds 128. This interactive experience solidifies understanding how each bit contributes to the total value and makes abstract concepts tangible. It also allows experimentation with two's complement, observing how the sign bit and other bits combine to represent negative numbers.

Frequently Asked Questions (FAQ)

What is the main difference between binary and hexadecimal?

The main difference lies in their base and the number of symbols they use. Binary (base 2) uses only two digits (0 and 1), making it the fundamental language of computers. Hexadecimal (base 16) uses sixteen symbols (0-9 and A-F) and is a more compact way to represent large binary numbers, often used for readability in programming and memory addressing.

Why are binary, octal, and hexadecimal important in computer science?

These number systems are crucial because computers internally operate using binary. Octal and hexadecimal provide human-friendly ways to represent and work with large binary values more efficiently than long strings of 0s and 1s. They simplify tasks like memory addressing, data representation, and debugging, making it easier for programmers to interact with low-level computer operations.

How does two's complement help represent negative numbers?

Two's complement is a method used by computers to represent negative numbers in binary. It simplifies arithmetic operations because addition and subtraction can be performed using the same circuitry for both positive and negative numbers. The most significant bit indicates the sign (0 for positive, 1 for negative), and the unique 'invert and add 1' process ensures that standard binary addition produces correct results for signed numbers within a fixed range.

Can I convert directly between octal and hexadecimal?

While not directly, you can easily convert between octal and hexadecimal by first converting to binary as an intermediate step. For instance, to convert octal to hexadecimal, you'd convert the octal number to binary (each octal digit to 3 binary bits), then group the binary bits into fours and convert each group to its hexadecimal equivalent. The reverse applies for hexadecimal to octal conversion.

Practise it, don't just read it

26 flashcards, a test with explanations and a podcast for this topic. The account is free and you pick up right where you left off.

  • No credit card
  • Web and mobile app
  • 330,000+ students
Start for free

Related topics