Salta al contenuto
Note per Studenti Number Systems and Base Conversions Report

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 rr the digits are 0,1,…,r−10,1,\dots,r-1. The value of a digit depends on its position. A number anan−1…a1a0.a−1…ama_na_{n-1}\dots a_1a_0.a_{-1}\dots a_m is worth N=∑i=mnai ri.N=\sum_{i=m}^{n}a_i\,r^i .

ana_n is the most significant digit (MSB in binary), ama_m the least significant (LSB); the point separates the integer part (exponents ≥0\ge0) from the fractional part.

Examples. 3458=3⋅82+4⋅8+5=22910345_8=3\cdot8^2+4\cdot8+5=229_{10}; 1A16=26101A_{16}=26_{10}; 2719.5310=2⋅103+7⋅102+1⋅10+9+5⋅10−1+3⋅10−22719.53_{10}=2\cdot10^3+7\cdot10^2+1\cdot10+9+5\cdot10^{-1}+3\cdot10^{-2}; 100112=16+2+1=191010011_2=16+2+1=19_{10}; 110101.112=32+16+4+1+12+14=53.7510110101.11_2=32+16+4+1+\tfrac12+\tfrac14=53.75_{10}.

exponent 0 1 2 3 4 5 6 7 8 9 10
2k2^k 1 2 4 8 16 32 64 128 256 512 1024

210≈1032^{10}\approx10^3 (kilo), 220≈1062^{20}\approx10^6, 230≈1092^{30}\approx10^9, 240≈10122^{40}\approx10^{12}. Disk makers use decimal prefixes (1 GB=1091\,\mathrm{GB}=10^9 byte), many operating systems binary ones (1 GB=2301\,\mathrm{GB}=2^{30} byte): a 500500 GB decimal disk is about 465.7465.7 GB binary.

Octal and hexadecimal

Since 8=238=2^3 and 16=2416=2^4, an octal digit is exactly 3 bits and a hexadecimal digit 4 bits. Octal digits are 00-77; hexadecimal digits are 00-99 and AA-FF for 1010-1515.

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: 0010 1100 0110 1011.1111 0000 01102=2C6B.F06160010\,1100\,0110\,1011.1111\,0000\,0110_2=2C6B.F06_{16}. Hexadecimal to binary: replace each digit by 4 bits: 3A6.C16=0011 1010 0110.11002=1110100110.1123A6.C_{16}=0011\,1010\,0110.1100_2=1110100110.11_2. Octal works the same with 3-bit groups: 7548=111 101 1002754_8=111\,101\,100_2.

Other examples: 127.48=64+16+7+48=87.510127.4_8=64+16+7+\tfrac48=87.5_{10}; B65F16=11⋅163+6⋅162+5⋅16+15=4668710B65F_{16}=11\cdot16^3+6\cdot16^2+5\cdot16+15=46687_{10}; 1011 01102=B616=182101011\,0110_2=B6_{16}=182_{10}.

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. 625−512=113625-512=113; 113−64=49113-64=49; 49−32=1749-32=17; 17−16=117-16=1; 1−1=01-1=0. So 625=29+26+25+24+20=10011100012625=2^9+2^6+2^5+2^4+2^0=1001110001_2.

Integer part, successive divisions by rr. Divide by the base, write the remainder, divide the quotient, until the quotient is 00. The first remainder is the least significant digit: read remainders bottom-up. (Each division removes the digit a0a_0 and shifts the others, like deleting the last decimal digit.)

625625 to binary: 625→312625\to312 r 11; 312→156312\to156 r 00; 156→78156\to78 r 00; 78→3978\to39 r 00; 39→1939\to19 r 11; 19→919\to9 r 11; 9→49\to4 r 11; 4→24\to2 r 00; 2→12\to1 r 00; 1→01\to0 r 11. Bottom-up: 100111000121001110001_2. 24712471 to octal: remainders 7,4,6,47,4,6,4, so 247110=464782471_{10}=4647_8 (check: 2048+384+32+72048+384+32+7).

Fractional part, successive multiplications by rr. 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 00 or enough digits.

41.12541.125: 41=101001241=101001_2; 0.125⋅2=0.250.125\cdot2=0.25 (0); 0.25⋅2=0.50.25\cdot2=0.5 (0); 0.5⋅2=1.00.5\cdot2=1.0 (1), stop. 41.12510=101001.001241.125_{10}=101001.001_2. 0.40.4: 0.80.8 (0); 1.61.6 (1); 1.21.2 (1); 0.40.4 (0): back to 0.40.4, so it repeats. 0.410=0.0110‾20.4_{10}=0.\overline{0110}_2 is periodic: not exactly representable with finitely many bits (so a computer cannot represent 0.10.1 or 0.40.4 exactly).

Elementary binary arithmetic

Sum. 0+0=00+0=0, 0+1=10+1=1, 1+0=11+0=1, 1+1=01+1=0 with carry 11; a column can add three 1s (1+1+1=11+1+1=1 carry 11). 011002+10001201100_2+10001_2 (12+1712+17) =111012=29=11101_2=29. 101102+10111210110_2+10111_2 (22+2322+23): columns from the right 11; 00 r1; 11 r1; 11; 00 r1; final carry 11: 1011012=45101101_2=45.

Difference. 0−0=00-0=0, 1−0=11-0=1, 1−1=01-1=0, 0−1=10-1=1 with a borrow: a 1 is taken from the next digit of the minuend, adding 22 to the current digit. 101102−10011210110_2-10011_2 (22−1922-19), per column with borrow in pp:

bit minuend subtrahend borrow in calculation digit borrow out
0 0 1 0 0−1<00-1<0: 2−1=12-1=1 1 1
1 1 1 1 1−1−1=−1<01-1-1=-1<0: 2−1=12-1=1 1 1
2 1 0 1 1−0−1=01-0-1=0 0 0
3 0 0 0 00 0 0
4 1 1 0 00 0 0

Result 000112=300011_2=3. If the minuend is smaller than the subtrahend, swap them and put a minus sign: 100112−111102=−010112=−111010011_2-11110_2=-01011_2=-11_{10}. 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. 10112×10121011_2\times101_2 (11×511\times5): partial products 10111011, 00000000 (shifted 1), 10111011 (shifted 2), sum 1101112=55110111_2=55.

Worked exercises

request result check
10110012→1011001_2\to dec 8989 64+16+8+164+16+8+1
387110→3871_{10}\to bin 1111000111112111100011111_2 2048+1024+512+256+16+8+4+2+12048+1024+512+256+16+8+4+2+1
E42916→E429_{16}\to bin 1110 0100 0010 100121110\,0100\,0010\,1001_2 584091058409_{10}
34710→347_{10}\to octal 5338533_8 5⋅64+3⋅8+35\cdot64+3\cdot8+3
100012+1111210001_2+1111_2 1000002=32100000_2=32 17+1517+15
100012−1111210001_2-1111_2 000102=200010_2=2 17−1517-15
100012×101210001_2\times101_2 10101012=851010101_2=85 17×517\times5

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: N=∑i=mnai riN=\sum_{i=m}^{n}a_i\,r^i; MSB = ana_n, LSB = ama_m. 3458=22910345_8=229_{10}, 1A16=26101A_{16}=26_{10}, 110101.112=53.75110101.11_2=53.75.
  • Powers of 2: 1,2,4,8,16,32,64,128,256,512,10241,2,4,8,16,32,64,128,256,512,1024; 210≈1032^{10}\approx10^3, 220≈1062^{20}\approx10^6, 230≈1092^{30}\approx10^9.
  • Octal/hex: 3 bits and 4 bits per digit; group from the radix point, pad outer groups with zeros: 0010 1100 0110 1011.1111 0000 01102=2C6B.F06160010\,1100\,0110\,1011.1111\,0000\,0110_2=2C6B.F06_{16}; 3A6.C16=1110100110.1123A6.C_{16}=1110100110.11_2; B65F16=46687B65F_{16}=46687.
  • Decimal to base rr, integer part: subtract powers of 2, or divide by rr and read remainders bottom-up (625=10011100012625=1001110001_2; 2471=464782471=4647_8).
  • Fractional part: multiply by rr, read integer parts top-down: 41.125=101001.001241.125=101001.001_2; 0.4=0.0110‾20.4=0.\overline{0110}_2 (periodic, not exact).
  • Sum: 1+1=01+1=0 carry 1; 1+1+1=11+1+1=1 carry 1; 101102+101112=1011012=4510110_2+10111_2=101101_2=45.
  • Difference: 0−10-1 needs a borrow (add 2 to the digit); 101102−100112=000112=310110_2-10011_2=00011_2=3. Minuend smaller: swap and put minus; computers use two's complement.
  • Product: shifted partial products (multiplicand or 0), AND gate per bit: 1011×101=1101112=551011\times101=110111_2=55.
  • Capacity: 11 GB decimal =109=10^9 B, binary =230=2^{30} B; 500500 GB decimal ≈465.7\approx465.7 GB binary.
  • Exercises: 10110012=891011001_2=89; 3871=11110001111123871=111100011111_2; E42916=58409E429_{16}=58409; 347=5338347=533_8.
  • Mistakes: remainders read top-down; fractions read bottom-up; grouping from the left; outer groups not padded.

Teoria collegata