Number Representation

COS2621 - Computer Organisation · Data Representation

Number Representation

Number representation is crucial in computer science. Computers use binary (base-2) number systems to represent data. This system uses only two digits: 0 and 1. Understanding how numbers are represented in binary helps you work with data at a fundamental level.

Binary Number System

The binary system is the foundation of computer data representation. Each digit in a binary number is a bit. A group of eight bits forms a byte. For example, the binary number 10110101 consists of eight bits.

Remember: 1 byte = 8 bits.

Converting Decimal to Binary

To convert a decimal number (base-10) to binary (base-2), you can use the division method. This method involves dividing the decimal number by 2 and recording the remainder. Follow these steps:

  1. Divide the decimal number by 2.
  2. Record the remainder (it will be 0 or 1).
  3. Update the decimal number to the quotient.
  4. Repeat steps 1-3 until the quotient is 0.
  5. The binary number is the remainders read in reverse order.

Example: Convert 13 to Binary

Let’s convert the decimal number 13 to binary:

Step 1: 13 ÷ 2 = 6 remainder 1
Step 2: 6 ÷ 2 = 3 remainder 0
Step 3: 3 ÷ 2 = 1 remainder 1
Step 4: 1 ÷ 2 = 0 remainder 1

Now, read the remainders in reverse order: 1101. Therefore, 13 in binary is 1101.

Watch out: Always read the remainders in reverse order. This is a common mistake.

Converting Binary to Decimal

To convert a binary number back to decimal, you can use the positional value method. Each bit in a binary number has a positional value based on its position from the right, starting at 0. The formula is:

Decimal value = bn × 2n + bn-1 × 2n-1 + ... + b0 × 20

Where bn is the bit value (0 or 1) at position n.

Example: Convert 1101 to Decimal

Let’s convert the binary number 1101 to decimal:

1 × 23 + 1 × 22 + 0 × 21 + 1 × 20
= 1 × 8 + 1 × 4 + 0 × 2 + 1 × 1
= 8 + 4 + 0 + 1
= 13

Therefore, the binary number 1101 is equal to 13 in decimal.

Hexadecimal Representation

Hexadecimal (base-16) is another number system used in computing. It uses sixteen symbols: 0-9 and A-F. Hexadecimal is often used because it is more compact than binary. Each hexadecimal digit represents four binary bits.

Remember: 1 hexadecimal digit = 4 binary bits.

Converting Decimal to Hexadecimal

To convert a decimal number to hexadecimal, you can use a method similar to the one used for binary conversion:

  1. Divide the decimal number by 16.
  2. Record the remainder.
  3. Update the decimal number to the quotient.
  4. Repeat until the quotient is 0.
  5. The hexadecimal number is the remainders read in reverse order.

Example: Convert 255 to Hexadecimal

Let’s convert the decimal number 255 to hexadecimal:

Step 1: 255 ÷ 16 = 15 remainder 15 (F)
Step 2: 15 ÷ 16 = 0 remainder 15 (F)

Now, read the remainders in reverse order: FF. Therefore, 255 in hexadecimal is FF.

Converting Hexadecimal to Decimal

To convert a hexadecimal number back to decimal, use the positional value method similar to binary:

Decimal value = hn × 16n + hn-1 × 16n-1 + ... + h0 × 160

Example: Convert 1A3 to Decimal

Let’s convert the hexadecimal number 1A3 to decimal:

1 × 162 + A × 161 + 3 × 160
= 1 × 256 + 10 × 16 + 3 × 1
= 256 + 160 + 3
= 419

Therefore, the hexadecimal number 1A3 is equal to 419 in decimal.

Character Encoding

Character encoding is the way characters are represented in binary. The most common encoding is ASCII (American Standard Code for Information Interchange). Each character is assigned a unique binary number. For example, the letter 'A' is represented as 65 in decimal, which is 01000001 in binary.

ASCII Table:

Here are some common ASCII characters and their binary representations:

CharacterDecimalBinary
A6501000001
B6601000010
C6701000011
04800110000
14900110001

Floating Point Representation

Floating point representation is used to represent real numbers (numbers with decimal points). It consists of three parts: the sign bit, the exponent, and the fraction (or mantissa). The IEEE 754 standard is commonly used for floating point representation.

In single precision (32 bits), the structure is as follows:

  • 1 bit for the sign
  • 8 bits for the exponent
  • 23 bits for the fraction

This allows for a wide range of values but comes with precision limitations.

Tip: Familiarise yourself with the IEEE 754 standard for floating point representation.

Summary

  • The binary number system uses bits (0 and 1) to represent data.
  • Decimal to binary conversion involves dividing by 2 and recording remainders.
  • Binary to decimal conversion uses positional values.
  • Hexadecimal is a base-16 system that is more compact than binary.
  • Character encoding, like ASCII, assigns binary numbers to characters.

Check your understanding

  1. Convert the decimal number 25 to binary.
  2. Convert the binary number 101010 to decimal.
  3. Convert the decimal number 100 to hexadecimal.
  4. What is the ASCII binary representation of the character 'Z'?