Positional Notation
A number in base with digits represents:
Each digit satisfies .
Common Number Systems
| Base | Name | Digits |
|---|---|---|
| 2 | Binary | 0, 1 |
| 8 | Octal | 0-7 |
| 10 | Decimal | 0-9 |
| 16 | Hexadecimal | 0-9, A-F |
Hexadecimal Digits
| Hex | Dec | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
Conversion: Base to Decimal
Expand using positional notation.
Example: to decimal
Example: to decimal
Conversion: Decimal to Base
Repeatedly divide by , record remainders (read bottom to top).
Example: 156 to binary
Answer:
Quick Conversions
Binary ↔ Octal
Group binary digits in 3s (from right).
Binary ↔ Hexadecimal
Group binary digits in 4s (from right).
Binary Arithmetic
Addition
1011
+ 0111
------
10010
Carry: 1 + 1 = 10 (write 0, carry 1)
Subtraction
Use borrowing or two's complement.
Multiplication
Similar to decimal, but simpler (only 0 and 1).
Signed Integer Representations
Sign-Magnitude
First bit is sign (0 = positive, 1 = negative), rest is magnitude.
Problem: Two representations of 0 (+0 and -0).
One's Complement
Negative: flip all bits.
Problem: Still two zeros.
Two's Complement (Most Common)
For -bit number:
- Positive: standard binary
- Negative: flip bits and add 1 (or )
Range: to
Example (8-bit):
- : flip → 11111010, add 1 →
Advantage: Single zero, addition works for signed numbers.
Converting Negative Numbers
Method 1: Flip bits, add 1 Method 2: in bits =
Example
in 8-bit two's complement:
Floating-Point Numbers
IEEE 754 Single Precision (32-bit)
| Sign | Exponent | Mantissa |
|---|---|---|
| 1 bit | 8 bits | 23 bits |
Value:
where = sign bit, = mantissa, = biased exponent.
Special Values
- Zero: ,
- Infinity: ,
- NaN: ,
Precision Limits
- Single (32-bit): ~7 decimal digits
- Double (64-bit): ~15 decimal digits
Bit Manipulation
Bitwise Operations
| Operation | Symbol | Example |
|---|---|---|
| AND | & | |
| OR | | | |
| XOR | ^ | |
| NOT | ~ |
Shift Operations
Left shift: multiplies by Right shift: divides by (for unsigned)
Common Tricks
Check if bit is set: (x >> k) & 1
Set bit : x | (1 << k)
Clear bit : x & ~(1 << k)
Toggle bit : x ^ (1 << k)
Check power of 2: (x & (x-1)) == 0
Powers of 2
| Value | |
|---|---|
| 1 | |
| 1,024 (1 KB) | |
| 1,048,576 (1 MB) | |
| 1,073,741,824 (1 GB) | |
| 4,294,967,296 | |
| 18,446,744,073,709,551,616 |