Introduction
Number systems are the foundation of how data is represented and processed in computers. Understanding different number systems, such as Binary, Octal, Decimal, and Hexadecimal, is crucial for working with computer systems and programming.
This chapter explores these number systems, their conversions, binary arithmetic, and the representation of negative and floating-point numbers.
Binary, Octal, Decimal, and Hexadecimal Systems
Number systems are defined by their base (or radix). The most common number systems used in computing are:
- Binary (Base-2): Uses digits 0 and 1. It is the fundamental language of computers.
- Octal (Base-8): Uses digits 0 to 7. It is often used as a shorthand for binary.
- Decimal (Base-10): Uses digits 0 to 9. It is the most familiar number system used in everyday life.
- Hexadecimal (Base-16): Uses digits 0 to 9 and letters A to F. It is widely used in programming and digital systems.
Each system has its own applications and advantages, depending on the context in which it is used.
Conversions Between Number Systems
Converting numbers between different bases is a common task in computing. Here are the methods for conversion:
Binary to Decimal
To convert a binary number to decimal, multiply each bit by 2 raised to the power of its position index (starting from 0 on the right) and sum the results.
Example: Convert 1011 (binary) to decimal.
1 * 2³ + 0 * 2² + 1 * 2¹ + 1 * 2⁰ = 8 + 0 + 2 + 1 = 11 (decimal)
Decimal to Binary
To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainders.
Example: Convert11(decimal) to binary. 11 ÷ 2 = 5 remainder 1 5 ÷ 2 = 2 remainder 1 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 Reading the remainders from bottom to top:1011(binary)
Hexadecimal to Decimal
To convert a hexadecimal number to decimal, multiply each digit by 16 raised to the power of its position index and sum the results.
Example: Convert 1A3 (hexadecimal) to decimal.
1 * 16² + 10 * 16¹ + 3 * 16⁰ = 256 + 160 + 3 = 419 (decimal)
Decimal to Hexadecimal
To convert a decimal number to hexadecimal, repeatedly divide the number by 16 and record the remainders.
Example: Convert419(decimal) to hexadecimal. 419 ÷ 16 = 26 remainder 3 26 ÷ 16 = 1 remainder 10 (A) 1 ÷ 16 = 0 remainder 1 Reading the remainders from bottom to top:1A3(hexadecimal)
Binary Arithmetic
Binary arithmetic involves performing addition, subtraction, multiplication, and division using binary numbers.
Binary Addition
Binary addition follows these rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 10 (carry 1)
Example: Add1011and1101. 1011 + 1101 ------ 11000
Binary Subtraction
Binary subtraction follows these rules:
- 0 - 0 = 0
- 1 - 0 = 1
- 1 - 1 = 0
- 0 - 1 = 1 (borrow 1)
Example: Subtract1101from10110. 10110 - 1101 ------ 01001
Binary Multiplication
Binary multiplication is similar to decimal multiplication.
Example: Multiply101by110. 101 * 110 ----- 000 101 101 ----- 11110
Binary Division
Binary division is also similar to decimal division.
Example: Divide1101by101. 101 ) 1101 101 ---- 001
Representation of Negative Numbers
Negative numbers in binary can be represented using three methods:
Signed Magnitude
In signed magnitude, the leftmost bit represents the sign (0 for positive, 1 for negative), and the remaining bits represent the magnitude.
Example: Represent-5in 8-bit signed magnitude. 5 in binary:00000101-5 in signed magnitude:10000101
1’s Complement
In 1’s complement, the negative number is obtained by inverting all the bits of the positive number.
Example: Represent-5in 8-bit 1’s complement. 5 in binary:00000101-5 in 1’s complement:11111010
2’s Complement
In 2’s complement, the negative number is obtained by inverting all the bits of the positive number and adding 1.
Example: Represent-5in 8-bit 2’s complement. 5 in binary:00000101Invert bits:11111010Add 1:11111011-5 in 2’s complement:11111011
Floating-Point Representation
Floating-point representation is used to store real numbers in computers. It consists of three parts:
- Sign bit: Represents the sign of the number (0 for positive, 1 for negative).
- Exponent: Represents the power of 2 to which the mantissa is raised.
- Mantissa: Represents the significant digits of the number.
Example: Represent13.75in 32-bit floating-point format. Step 1: Convert to binary:1101.11Step 2: Normalize:1.10111 * 2^3Step 3: Sign bit: 0 (positive) Step 4: Exponent: 3 + 127 (bias) = 130 =10000010Step 5: Mantissa:10111000000000000000000Final representation:0 10000010 10111000000000000000000
Conclusion
Understanding number systems and data representation is essential for working with computers and digital systems. From binary arithmetic to floating-point representation, these concepts form the backbone of computer science and engineering. By mastering these topics, you can gain a deeper insight into how data is processed and stored in modern computing systems.