CS1101: Lecture 11 Binary Numbers
Total Page:16
File Type:pdf, Size:1020Kb
Lecture Outline CS1101: Lecture 11 Binary Numbers Dr. Barry O’Sullivan • The General Form of a Decimal Number [email protected] • Some Radix Systems • Examples of Radix Numbers • Conversions: Binary, Octal, Hexadecimal • Examples: Conversion • Decimal-Binary Conversion by Halving Course Homepage • Reading: Tanenbaum, Appendix A. http://www.cs.ucc.ie/˜osullb/cs1101 Department of Computer Science, University College Cork Department of Computer Science, University College Cork 1 CS1101: Systems Organisation Binary Numbers CS1101: Systems Organisation Binary Numbers The General Form of a Decimal Number Some Radix Systems 100's 10's 1's .1's .01's .001's place place place place place place • A radix k number system requires k different symbols to represent the digits 0 to k − 1. dn …d2 d1 d0 . d–1 d–2 d–3 • Decimal numbers are built up from the 10 decimal digits n i Number = Σ di × 10 i = –k 0123456789 Figure A.1 The general form of a decimal number • Binary numbers do not use these ten digits. They are all constructed exclusively from the two binary digits 01 • Octal numbers are built up from the eight octal digits 01234567 Department of Computer Science, University College Cork 2 Department of Computer Science, University College Cork 3 Some Radix Systems Examples of Radix Numbers • For hexadecimal numbers, 16 digits are needed. Thus six new symbols are required. Binary 1 1 1 1 10 1 00 0 1 It is conventional to use the upper case letters 1 × 210 + 1 × 29 + 1 × 28 + 1 × 27 + 1 × 26 + 0 × 25 + 1 × 24 + 0 × 23 + 0 × 22 + 0 × 21 + 1 × 20 A through F for the six digits following 9. 1024 + 512 +256 + 128 + 64 +0 +16 + 0 + 0 + 0 + 1 Octal 3 7 2 1 Hexadecimal numbers are then built up from 3 × 83 + 7 × 82 + 2 × 81 + 1 × 80 the digits 1536 + 448 + 16 + 1 Decimal 2 0 0 1 2 × 103 + 0 × 102 + 0 × 101 + 1 × 100 0123456789ABCDEF 2000+ 0 + 0 + 1 Hexadecimal 7D 1 . 7 × 162 + 13 × 161 + 1 × 160 1792+208 + 1 • Examples of radix numbers: – 11 – 19 Figure A.2 The number 2001 in binary, octal and – 7B9 hexidecimal. – 1011011 • To avoid ambiguity, people use a subscript or 2, 8, 10 or 16 to indicate the radix when it is not obvious from the context. • The representation of numbers may be different when using a different radix. Department of Computer Science, University College Cork 4 Department of Computer Science, University College Cork 5 CS1101: Systems Organisation Binary Numbers CS1101: Systems Organisation Binary Numbers Conversions: Binary, Octal, Hexadecimal Conversions: Binary, Octal, Hexadecimal • Conversion between octal or hexadecimal • Conversion from octal to binary is equally numbers and binary numbers is easy. trivial. • To convert a binary number to octal divide it • Each octal digit is simply replaced by the into groups of 3 bits, with the 3 bits immediately equivalent 3-bit binary number. to the left (or right) of the decimal point (often called a binary point) forming one group, the • Conversion from hexadecimal to binary is 3 bits immediately to their left, another group, essentially the same as octal-to-binary except and so on. that each hexadecimal digit corresponds to a group of 4 bits instead of 3 bits. • Each group of 3 bits can be directly converted to a single octal digit, 0 to 7, according to the conversion given in the first lines of Figure A-3. • It may be necessary to add one or two leading or trailing zeros to fill out a group to 3 full bits. Department of Computer Science, University College Cork 6 Department of Computer Science, University College Cork 7 Examples: Conversion Decimal-Binary Conversion Example 1 . • Conversion of decimal numbers to binary Hexadecimal 1 9 4 8 B 6 can be done in two different ways. Binary 0 0 0 1 1 0 0 1 0 1 0 0 1 0 0 0 . 1 0 1 1 0 1 1 0 0 Octal 1 4 5 1 0 . 5 5 4 • The first method follows directly from the definition of binary numbers. Example 2 • Hexadecimal 7 BCA 3 . B 4 The largest power of 2 smaller than the number Binary 0 1 1 1 1 0 1 1 1 0 1 0 0 0 1 1 . 1 0 1 1 1 1 0 0 0 1 0 0 is subtracted from the number. Octal 75 6 4 3 . 5 7 0 4 • The process is then repeated on the difference. Figure A.4 Examples of octal-to-binary and • Once the number has been decomposed hexademcimal-binary conversion into powers of 2, the binary number can be assembled with 1s in the bit positions corresponding to powers of 2 used in the decomposition, and Os elsewhere. • What is the binary representation of the decimal number 20? 4 2 2010 =16+4=2 + 2 = 101002 Department of Computer Science, University College Cork 8 Department of Computer Science, University College Cork 9 CS1101: Systems Organisation Binary Numbers CS1101: Systems Organisation Binary Numbers Decimal-Binary Conversion by Halving Decimal-Binary Conversion by Halving Quotients Remainders • This other method (for integers only) consists of dividing the number by 2. 1 4 9 2 7 4 6 0 • The quotient is written directly beneath the 3 7 3 0 original number and the remainder, 0 or 1, is 1 8 6 1 written next to the quotient. 9 3 0 4 6 1 • The quotient is then considered and the 2 3 0 process repeated until the number 0 has been 1 1 1 reached. 5 1 2 1 • The result of this process will be two columns 1 0 of numbers, the quotients and the remainders. 0 1 • The binary number can now be read directly 1 0 1 1 1 0 1 0 1 0 0 = 149210 from the remainder column starting at the bottom. Figure A.5 Conversion of the decimal number 1492 to binary by successive halving. Department of Computer Science, University College Cork 10 Department of Computer Science, University College Cork 11.