Number Systems and Base Conversions
In questa pagina 6
A computer works in binary, but numbers are written more conveniently in octal or hexadecimal. This note covers conversions and elementary binary arithmetic.
Positional notation
Definition. In base the digits are . The value of a digit depends on its position. A number is worth
is the most significant digit (MSB in binary), the least significant (LSB); the point separates the integer part (exponents ) from the fractional part.
Examples. ; ; ; ; .
| exponent | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024 |
(kilo), , , . Disk makers use decimal prefixes ( byte), many operating systems binary ones ( byte): a GB decimal disk is about GB binary.
Octal and hexadecimal
Since and , an octal digit is exactly 3 bits and a hexadecimal digit 4 bits. Octal digits are -; hexadecimal digits are - and - for -.
| dec | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| bin | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
| hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
Binary to hexadecimal: form groups of 4 bits starting from the radix point (leftwards for the integer part, rightwards for the fractional part), padding the outer groups with zeros, and replace each group by its digit: . Hexadecimal to binary: replace each digit by 4 bits: . Octal works the same with 3-bit groups: .
Other examples: ; ; .
Decimal to binary (or other bases)
Integer part, subtracting powers of 2. Take the largest power of 2 not exceeding the number, subtract it, repeat on the remainder. ; ; ; ; . So .
Integer part, successive divisions by . Divide by the base, write the remainder, divide the quotient, until the quotient is . The first remainder is the least significant digit: read remainders bottom-up. (Each division removes the digit and shifts the others, like deleting the last decimal digit.)
to binary: r ; r ; r ; r ; r ; r ; r ; r ; r ; r . Bottom-up: . to octal: remainders , so (check: ).
Fractional part, successive multiplications by . Multiply the fractional part by 2; the integer part of the product (0 or 1) is the next digit after the point, starting from the most significant; repeat on the new fractional part until or enough digits.
: ; (0); (0); (1), stop. . : (0); (1); (1); (0): back to , so it repeats. is periodic: not exactly representable with finitely many bits (so a computer cannot represent or exactly).
Elementary binary arithmetic
Sum. , , , with carry ; a column can add three 1s ( carry ). () . (): columns from the right ; r1; r1; ; r1; final carry : .
Difference. , , , with a borrow: a 1 is taken from the next digit of the minuend, adding to the current digit. (), per column with borrow in :
| bit | minuend | subtrahend | borrow in | calculation | digit | borrow out |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | : | 1 | 1 |
| 1 | 1 | 1 | 1 | : | 1 | 1 |
| 2 | 1 | 0 | 1 | 0 | 0 | |
| 3 | 0 | 0 | 0 | 0 | 0 | |
| 4 | 1 | 1 | 0 | 0 | 0 |
Result . If the minuend is smaller than the subtrahend, swap them and put a minus sign: . Computers do not do this: they use two's complement (Signed Numbers, Complement Arithmetic and OverflowUnsigned subtraction is correct without borrow out; otherwise the result M-N+2^n is wrong. One's complement is 2^n-1-N (bitwise inversion); two's complement is 2^n-N (one's complement plus 1). Signed numbers: sign-magnitude (two zeros, symmetric range) or two's complement (one zero, range -2^(n-1) to 2^(n-1)-1, MSB weight -2^(n-1)). In two's complement addition and subtraction are the same adder: A-B = A + not B + 1, carry out discarded. Overflow: unsigned if carry out is 1 in an addition; signed if the carry into the sign bit differs from the carry out of it.Signed Numbers, Complement Arithmetic and Overflow →).
Product. Each partial product is the multiplicand (multiplier bit 1) or zero (bit 0); partial products are shifted left and added. In hardware the product of two bits is an AND gate. (): partial products , (shifted 1), (shifted 2), sum .
Worked exercises
| request | result | check |
|---|---|---|
| dec | ||
| bin | ||
| bin | ||
| octal | ||
Common mistakes
- Reading division remainders top-down (they go bottom-up), or fractional digits bottom-up (they go top-down).
- Grouping bits from the left instead of from the radix point, or not padding the outer groups with zeros.
- Confusing decimal and binary GB.
Versione ripasso
- Positional notation: ; MSB = , LSB = . , , .
- Powers of 2: ; , , .
- Octal/hex: 3 bits and 4 bits per digit; group from the radix point, pad outer groups with zeros: ; ; .
- Decimal to base , integer part: subtract powers of 2, or divide by and read remainders bottom-up (; ).
- Fractional part: multiply by , read integer parts top-down: ; (periodic, not exact).
- Sum: carry 1; carry 1; .
- Difference: needs a borrow (add 2 to the digit); . Minuend smaller: swap and put minus; computers use two's complement.
- Product: shifted partial products (multiplicand or 0), AND gate per bit: .
- Capacity: GB decimal B, binary B; GB decimal GB binary.
- Exercises: ; ; ; .
- Mistakes: remainders read top-down; fractions read bottom-up; grouping from the left; outer groups not padded.