Comprehensive Notes on Number Bases, Conversions, Two's Complement, Floating Point, Modulo, and Set Theory
Number Bases and Systems
Introduction to Number Bases
Concept of Grouping: Numbers are represented by grouping quantities. The base of a number system, also known as its radix, defines the size of these groups and the number of unique digits available in that system.
In Base 10 (decimal), we use ten unique digits (0-9). Each digit's position corresponds to a power of 10. For example, in the number , the leftmost '2' represents (twenty), and the rightmost '2' represents (two).
Example of place value, where digits represent multiples of powers of the base.
- (Base 10)
- (Base 10)
- (Base 10)Conversion via Grouping (Example: to Base-6): This method involves conceptually forming groups of the target base's powers. Instead of repeated division, you directly determine coefficients for each power.
Determine how many groups of fit into the number, progressing through powers of the base.
Step 1: Find the largest power of 6 less than or equal to 250. , . So we start with .
How many groups of in 250? ( with remainder ).
The first digit (coefficient for ) is 6.
Step 2: With the remainder (34), how many groups of ?
( with remainder ).
The second digit (coefficient for ) is 5.
Step 3: With the remaining remainder (4), how many groups of ?
( with remainder ).
The third digit (coefficient for ) is 4.
Result: Combining the coefficients, .
Common Number Bases
Base 10 (Decimal): Uses digits 0-9. Our everyday number system. Each position represents a power of 10. Primarily used for human calculations and general measurement.
Base 2 (Binary): Uses digits 0-1. Fundamental for computers and digital electronics, where information is represented as electrical signals (on/off, high/low voltage).
Base 8 (Octal): Uses digits 0-7. Sometimes used as a compact representation of binary numbers (three binary bits map to one octal digit), particularly in older computing systems.
Base 16 (Hexadecimal): Uses digits 0-9 and letters A-F (where A=10, B=11, C=12, D=13, E=14, F=15). Widely used in computing for representing memory addresses, color codes (e.g., #FFFFFF), and other binary data in a more human-readable form, as four binary bits map to one hexadecimal digit.
Often denoted with prefixes like
0xorh(e.g.,0xA1orA1h).
Core Rules and Constraints
Largest Digit Allowed: In any column (place value), the largest digit permitted is one less than the base. This is because once you reach a value equal to the base, you form a group of that base and carry it over to the next higher place value.
Formula: (where is the largest valid digit in a given base and is the base itself).
Column Values (Place Values): Each position in a number represents a power of the base. Moving from right to left, the power of the base increases by one for each position. For fractional parts, moving right from the decimal point, the power of the base decreases by one.
Base 2: (and for fractions, )
Base 8:
Base 16:
Memorization: It is required to memorize the hexadecimal and binary equivalents for decimal numbers 0-15. This is crucial for rapid conversion between these common bases.
Decimal | Binary | Hexadecimal
---|---|---
0 | 0000 | 0
1 | 0001 | 1
2 | 0010 | 2
3 | 0011 | 3
4 | 0100 | 4
5 | 0101 | 5
6 | 0110 | 6
7 | 0111 | 7
8 | 1000 | 8
9 | 1001 | 9
10 | 1010 | A
11 | 1011 | B
12 | 1100 | C
13 | 1101 | D
14 | 1110 | E
15 | 1111 | F
Number Base Conversions
1. Converting from Base-N to Decimal (Base-10)
Method: To convert any number from an arbitrary base-N to decimal, you expand the number using the definition of place values. Each digit is multiplied by its corresponding column value (power of the base), and these products are then summed.
Formula: For a number in base , its decimal value is: For numbers with a fractional part, the formula extends to negative powers:
Steps:
Write down the number from left to right.
Assign column values (powers of the base) to each digit, starting from for the rightmost integer digit and increasing by 1 for each position to the left. For fractional parts, start with for the first digit after the decimal point, decreasing by 1 for each position to the right.
Multiply each digit by its respective column value.
Sum all the products to get the decimal equivalent.
Examples:
Convert to Decimal:
Convert to Decimal:
Convert to Decimal:
Convert to Decimal: (Remember C=12, D=13)
2. Converting from Decimal (Base-10) to Base-N
Method (Division Method for Integers): This method involves repeatedly dividing the decimal number by the target base and noting the remainders. This works because each remainder represents the coefficient of a power of the base, starting from (rightmost digit).
Steps:
Divide the decimal number by the target base.
Record the remainder. This remainder will be the rightmost digit (least significant digit) in the new base.
Use the quotient from the previous step as the new number to divide.
Repeat steps 1-3 until the quotient is 0.
The new base representation is the sequence of remainders, read from bottom to top (last remainder to first, which corresponds to the most significant digit to the least significant digit).
Examples:
Convert to Base-6:
R
R
R
R
Result (read remainders upwards):
Convert to Binary:
R
R
R
R
R
R
R
R
Result:
Convert to Base-7:
R
R
R
Result:
3. Converting Decimal Fractions to Another Base
Method (Multiplication Method for Fractions): This method involves repeatedly multiplying the fractional part of the decimal number by the target base. Each integer part generated becomes the next digit in the new base's fractional representation. This process extracts the digits from most significant to least significant after the decimal point.
Steps:
Multiply the decimal fractional part by the target base.
The integer part of the result is the next digit in the new base (most-significant first).
Take the remaining fractional part of the product and use it for the next step.
Repeat until the fractional part is 0 or the desired level of precision is reached. Be aware that some decimal fractions may result in non-terminating (repeating) fractions in other bases.
Examples:
Convert to Binary (four decimal places):
(integer part: 0)
(integer part: 0)
(integer part: 0)
(integer part: 1)
(integer part: 1)
(integer part: 1)
(integer part: 1)
(integer part: 0)
- (integer part: 1)
(Continuing this process, you may find it to be a repeating binary fraction.)
Result:
Convert to Base-5:
(integer part: 0)
(integer part: 3)
(integer part: 0)
(This pattern will repeat.)
Result:
4. Converting Fractional Binary to Decimal
Method: Similar to converting integers, multiply each fractional digit by its corresponding negative power of 2 and sum the products.
Formula: For a fractional digit in binary, it represents .
Example: Convert to Decimal:
5. Shortcut Conversions (Binary, Octal, Hexadecimal)
These bases are related by powers of 2, allowing for direct grouping of bits, which significantly simplifies conversions without needing to go through decimal.
Binary and Hexadecimal are related:
One hexadecimal digit represents exactly four binary bits (). This relationship makes conversions very fast.
Binary to Hex: To convert, group the binary digits into sets of four, starting from the right for the integer part and from the left for the fractional part (pad with leading/trailing zeros if necessary to complete a group of four). Then convert each group to its single hex equivalent using the memorized table.
Example:
Hex to Binary: Convert each hex digit into its four-bit binary equivalent from the memorized table. Concatenate these binary groups to form the full binary number.
Example:
Binary and Octal are related:
One octal digit represents exactly three binary bits (). This similarly allows for direct grouping.
Binary to Octal: Group binary digits into sets of three, starting from the right for the integer part and from the left for the fractional part (pad with leading/trailing zeros if necessary). Convert each group to its octal equivalent.
Example:
Octal to Binary: Convert each octal digit into its three-bit binary equivalent. Concatenate these binary groups.
Example:
Arithmetic in Different Bases
1. Addition in Different Bases
Rule: Perform addition column by column, starting from the rightmost digit, just as you would in base 10. If the sum in a column equals or exceeds the base, you perform a carry. The value carried to the next column is exactly 1 (representing one group of the base), and the digit in the current column is the remainder after subtracting the base from the sum.
Example (Conceptual): In base 6, if you add
in decimal.
Since is greater than or equal to the base (6), we subtract the base: .
We carry 1 to the next column (which doesn't exist here, so it becomes the leading digit).
The result is (carry 1, current digit 2).
Example: Add :
Rightmost column ($6^0$): . (No carry)
Middle column ($6^1$): . Write down 2, carry 1.
Leftmost column ($6^2$): . Write down 0, carry 1.
Result: The carries continue to the next column forming a leading digit. So, it's .
2. Subtraction in Different Bases
Rule: Perform subtraction column by column, starting from the rightmost digit. If a digit in the top number is too small to subtract from, you must borrow from the next higher column. When you borrow 1 from a column (say, ), it adds a value equal to the base () to the current column (say, ). The digit in the column from which you borrowed is reduced by 1.
Example (Conceptual): In base 6, if you need to subtract from a top digit of in a column:
You would borrow 1 from the digit to its left.
That borrowed 1 (from the position) becomes in the current column.
So, in the current column, you now effectively have .
Then, you subtract: .
The digit in the column from which you borrowed would be reduced by 1.
Binary Subtraction: The fundamental rules are simplified due to only two digits:
(This requires a borrow from the left. When you borrow 1 from the next position, it becomes in the current position. So, ).
Modulo Operation (a mod m)
Definition: The modulo operation, , calculates the remainder when an integer (the dividend) is divided by another integer (the divisor), where must be greater than 0. The result is always non-negative and less than .
Formula (Relationship): The division algorithm states that for any integers and with m > 0, there exist unique integers (quotient) and (remainder) such that , where 0 \le r < m. The modulo operation finds this , so .
Applications: Detecting divisibility (if , then is divisible by ), cyclic counting (e.g., hours on a clock, days of the week), modular arithmetic in cryptography, hashing functions, and understanding fixed-width arithmetic (how numbers 'wrap around' on overflow).
Examples:
(because )
(Useful for checking if a number is odd (1) or even (0)).
(because ; the remainder must be non-negative).
(because )
Powers of Two and Metric Prefixes
Powers of 2 to Memorize: These are fundamental in computer science and often appear in various contexts related to memory, data storage, and processing.
Metric Prefixes for Powers of 2 (approximate powers of 10): In computing, these prefixes often refer to powers of 2 (binary prefixes, sometimes called IEC prefixes) rather than exact powers of 10, though they are often used interchangeably in approximate contexts due to their proximity.
(often referred to as 1 Kilobyte (KB) or more precisely 1 Kibibyte (KiB) - approximately )
(often referred to as 1 Megabyte (MB) or more precisely 1 Mebibyte (MiB) - approximately )
(often referred to as 1 Gigabyte (GB) or more precisely 1 Gibibyte (GiB) - approximately )
(often referred to as 1 Terabyte (TB) or more precisely 1 Tebibyte (TiB) - approximately )
(often referred to as 1 Petabyte (PB) or more precisely 1 Pebibyte (PiB) - approximately )
Shortcut for large powers: To quickly estimate large powers of 2, you can use the relationship with the kilo, mega, giga prefixes:
(e.g., ; )
Binary Number Representation and Limits
1. Unsigned Binary Numbers
Definition: In unsigned binary representation, all bits in a number are used to represent the magnitude of the value. This means numbers are always considered non-negative and can only represent zero or positive integers.
Storage Limits for bits:
Number of distinct values: (each bit can be 0 or 1, and there are bits).
Smallest value: (all bits are 0).
Largest value: (all bits are 1).
Example: With 8 bits:
Range: to .
So, an 8-bit unsigned integer can represent any integer from 0 to 255, inclusive.
2. Signed Binary Numbers (Two's Complement)
Definition: Two's Complement is the most common and efficient method used by modern computers to represent signed integers (positive, negative, and zero). Its key advantages include a single representation for zero and simplified arithmetic operations (addition and subtraction can be performed using the same hardware).
The most significant bit (MSB) acts as the sign bit: 0 for positive numbers and 1 for negative numbers. However, for negative numbers, the remaining bits are not simply the magnitude of the number.
Storage Limits for bits:
Number of distinct values: (same as unsigned).
Smallest (most negative) value: (when the MSB is 1 and all other bits are 0).
Largest (most positive) value: (when the MSB is 0 and all other bits are 1).
Example: With 8 bits:
Range: to = to = to .
3. Converting Decimal to Two's Complement
For Positive Numbers: Convert the positive decimal number to its binary representation normally. Then, pad with leading zeros to meet the specified number of bits ().
Example: in 8 bits is .
Example: in 8 bits is .
For Negative Numbers (Steps): This is a three-step process:
Take the absolute value of the decimal number and convert it to its binary representation using the specified number of bits ().
Invert all the bits (0s become 1s, and 1s become 0s). This result is known as the one's complement.
Add 1 to the inverted result. Any carry-out from the most significant bit is discarded.
Example: Convert to 8-bit Two's Complement:
Absolute value: converted to 8-bit binary is .
Invert all bits (one's complement): .
Add 1: .
Result:
4. Converting Two's Complement to Decimal
For Positive Numbers (Leading bit is 0): If the most significant bit is 0, the number is positive. Convert the binary number to decimal normally, just as you would for an unsigned binary number.
Example: .
For Negative Numbers (Leading bit is 1): If the most significant bit is 1, the number is negative. Use one of two methods:
Method 1 (Invert and Add 1):
a. Invert all the bits (take the one's complement).
b. Add 1 to the inverted result.
c. Convert the resulting positive binary number to decimal.
d. Place a negative sign in front of the decimal value to get the final answer.Method 2 (Weighted Sum with Negative MSB): Treat the MSB's place value as negative. For an -bit two's complement number , the decimal value is .
Example: Convert (8-bit Two's Complement) to Decimal (using Method 1):
Invert bits:
Add 1:
Convert to decimal: .
Place a negative sign: .
Result:
Floating-Point Representation (IEEE 754 Standard)
Floating-point representation is a method to encode real numbers (numbers with fractional parts) within a fixed number of bits, allowing for a wide dynamic range at the cost of precision. The IEEE 754 standard is the most common standard for floating-point arithmetic.
1. General Structure
Floating-point numbers (real numbers) are typically stored using three main parts:
Sign Bit (S): A single bit that determines whether the number is positive (0) or negative (1).
Exponent (E): Represents the magnitude of the number. To handle both positive and negative exponents conveniently without needing a separate sign bit for the exponent, a bias is added to the actual exponent. The stored exponent is therefore the true exponent plus a fixed bias. For single-precision (32-bit) floats, the bias is 127; for double-precision (64-bit), it's 1023.
Mantissa / Significand (M): Represents the precision bits of the number. In the IEEE 754 standard, the significand is always normalized, meaning it is represented in the form where is the fractional part stored. The leading '1' before the decimal point is implicit (not stored) to save a bit, except for denormalized numbers.
2. Format Details (IEEE 754)
Single-Precision (32-bit floating-point number):
1 bit for the Sign (S)
8 bits for the Exponent (E), with a bias of
23 bits for the Mantissa/Significand (M), providing a precision equivalent to about 7 decimal digits.
The value is calculated as:
Double-Precision (64-bit floating-point number):
1 bit for the Sign (S)
11 bits for the Exponent (E), with a bias of
52 bits for the Mantissa/Significand (M), providing a precision equivalent to about 15-17 decimal digits.
The value is calculated as:
3. Special Cases
The IEEE 754 standard defines specific combinations of the exponent and mantissa to represent special values:
Zero (0): Represented by an exponent of all zeros and a mantissa of all zeros. Both +0 and -0 exist.
Infinity ($\infty$): Represented by an exponent of all ones and a mantissa of all zeros. Used for results that overflow the representable range (e.g., division by zero).
Denormalized Numbers: Represented by an exponent of all zeros and a non-zero mantissa. They allow for representing numbers even closer to zero than normalized numbers, filling the underflow gap. The implicit leading '1' is dropped, and the exponent is fixed at the minimum valid exponent ().
Not a Number (NaN): Represented by an exponent of all ones and a non-zero mantissa. Used to represent results of invalid or unrepresentable operations (e.g., , ).
This detailed structure allows for efficient and standardized representation and arithmetic of a wide range of real numbers in computing systems.