CPSC 121: Module 03 - Number Representation
CPSC 121: Module 03 - Number Representation
Overview of Number Representation
Numbers can be represented as a sum of products.
Each term consists of a digit multiplied by a power of a base.
Decimal Representation (Base 10)
Digits range from 0 to 9.
Commonly used due to humans typically counting with ten fingers.
Example:
Binary Representation (Base 2)
Computers operate in binary because they recognize only two symbols: 0 and 1.
Each binary digit (bit) corresponds to a power of 2.
Notation for base: a subscript indicates the base of a number.
Example:
Other Number Bases
Hexadecimal Representation (Base 16)
Includes digits from 0 to 9 and letters A to F (representing 10 to 15).
Notation for hexadecimal includes a prefix
0x.Example:
Purpose: making binary numbers more human-readable.
Conversion Between Bases
Direct conversion between hexadecimal and binary is a standard practice.
For binary to hexadecimal conversion, group binary bits into sets of four:
Example Grouping:
If the total number of bits is not a multiple of four, leading zeros are added.
Binary to Decimal Conversion
Process: Multiply each bit by the corresponding power of 2 and sum the results.
Example:
Decimal to Binary Conversion
Find the largest power of 2 less than or equal to the number, subtract, and repeat until the sum equals the original number.
Example:
Adding Binary Numbers
Rules for binary addition resemble decimal addition:
0 + 0 = 0
1 + 0 = 1
1 + 1 = 10 (carry over)
Carry propagates to the next column.
Example addition:
Carrying process demonstrated through binary digits.
Signed and Unsigned Integers
Unsigned integers: Only represent non-negative integers.
Signed integers: Use leftmost bit to indicate the sign (0 for positive, 1 for negative).
Example for sign magnitude:
Positive 21:
Negative 21:
Two's Complement Representation
Negative numbers are represented by two's complement: invert bits and add 1.
Example:
Two's complement of 11100010 = 00011101 + 1 = 00011110.
Positive numbers have a leading zero; negative numbers have a leading one.
Advantages: subtraction is achieved through addition of two's complements.
Example: Subtraction as addition:
Example of results in .
Handling Two's Complement
To reverse a two's complement operation, either flip and add 1 or subtract 1 and flip.
All modern systems utilize two's complement for its simplicity and efficiency.
Real Numbers Representation
Real numbers are expressed with both integer and fractional parts in binary.
Integer part utilizes powers of 2 counting up, fractional parts count down from 2^{-1}.
Example:
Converting decimal reals to binary:
Integer part conversion through division.
Fractional part conversion via multiplication.
Example with 13.90625:
Integer: 13 (binary: 1101)
Fraction: .90625 (binary: .11101)
Complete binary: 1101.11101
Scientific Notation in Binary
Binary scientific notation uses powers of 2.
Format:
1.xxxxx * 2^ewherexis the mantissa andeis the exponent.Example:
Important components for storage:
Exponent: a signed integer.
Mantissa: number after the decimal.
Sign: determines positivity or negativity.
Modular Arithmetic
Classifying integers based on remainders after division by a number m.
Notation for modulo: meaning remainder r when x is divided by m.
Example: (because 27 divided by 4 has a remainder of 3).
Congruence relation: if two integers leave the same remainder when divided by m, they belong to the same equivalence classes.
Example: .
Fundamental Theorem of Modular Arithmetic
If and are known, expressions with these can be simplified:
yield the same result whether mod is taken before or after operations.
Example:
produces the same result as .