<<

CS1101: Systems Organisation Radix : Conversions Lecture Outline CS1101: Lecture 15 Radix Numbers: Conversions

Dr. Barry O’Sullivan Reading: Tanenbaum, Appendix A. [email protected] Conversions: Binary, ,

Examples: Conversion

Decimal-Binary Conversion

Decimal-Binary Conversion by Halving

Binary-Decimal Conversion

Course Homepage Binary-Decimal Conversion by Doubling http://www.cs.ucc.ie/˜osullb/cs1101

Decimal-Octal & Decimal-Hexadecimal

Department of Computer Science, University College Cork Department of Computer Science, University College Cork 1

2 CS1101: Systems Organisation Radix Numbers: Conversions ersions v Conversions: Binary, Octal, Hexadecimal Con Numbers: Radix

Conversion between octal or hexadecimal s numbers and binary numbers is easy. 0 xidecimal. 2

× 1 1 he +

+ 1

1 2

0

× To convert a binary to octal divide it 0 and + + 0 Number 2 into groups of 3 bits, with the 3 bits immediately 2 0 ×

0 to the left (or right) of the decimal point (often + + 0 3 2 octal

0 × called a binary point) forming one group, the , 0 y + + 0 4 Radix 3 bits immediately to their left, another group, 2 1 × 16

k and so on. of + + 1 5 binar 2 Cor × 0 0

in

+ 0 + 6

2 Each group of 3 bits can be directly converted

. × 1 College 64 0

+ 1 to a single octal digit, 0 to 7, according to the + 10 7 2001

× 2 0 ersity × 1 1 8 conversion given in the first lines of Figure A-3. 128 × 1 1 + 0 + 1 1 1 + 1 + 16

8 Univ + 10

× + 1 2 ,

Examples 1 × 1 × 1 8

0 0 256 2 × 16

+

1

+ 1 + 1 umber + 0 + + 1 2

+ It may be necessary to add one or two leading 9 16 n

+ 2 2 2 10 ×

Science 8 ×

× 1 7 × D 208 512 0 0 or trailing zeros to fill out a group to 3 full bits. 448 + + + + 0 + 13 + 1 + 3 2 + 7 10 3 The 2 16 8 10 Organisation

7 3 1 × × × × 2 1024 1536 1792 7 1

3 2 2000 Computer of A.2 Systems y : xadecimal tment Binar Octal Decimal He CS1101 Depar

Figure Department of Computer Science, University College Cork 3 CS1101: Systems Organisation Radix Numbers: Conversions CS1101: Systems Organisation Radix Numbers: Conversions Conversions: Binary, Octal, Hexadecimal Examples: Conversion

Example 1 1 9 4 8 . B 6 Conversion from octal to binary is equally Hexadecimal 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 trivial. Binary Octal 1 4 5 1 0 . 5 5 4

Each octal digit is simply replaced by the

equivalent 3-bit . Example 2 Hexadecimal 7 B A 3 . B C 4 Conversion from hexadecimal to binary is 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 essentially the same as octal-to-binary except Octal 7 5 6 4 3 . 5 7 0 4 that each hexadecimal digit corresponds to a group of 4 bits instead of 3 bits. Figure A.4 Examples of octal-to-binary and hexademcimal-binary conversion

Department of Computer Science, University College Cork 4 Department of Computer Science, University College Cork 5

CS1101: Systems Organisation Radix Numbers: Conversions CS1101: Systems Organisation Radix Numbers: Conversions Decimal-Binary Conversion Decimal-Binary Conversion by Halving

Conversion of decimal numbers to binary can be done in two different ways. This other method (for integers only) consists of dividing the number by 2.

The first method follows directly from the definition of binary numbers. The quotient is written directly beneath the original number and the remainder, 0 or 1, is The largest power of 2 smaller than the number written next to the quotient. is subtracted from the number.

The quotient is then considered and the

The process is then repeated on the difference. process repeated until the number 0 has been reached. Once the number has been decomposed

into powers of 2, the binary number can The result of this process will be two columns be assembled with 1s in the bit positions of numbers, the quotients and the remainders. corresponding to powers of 2 used in the decomposition, and Os elsewhere. The binary number can now be read directly

What is the binary representation of the from the remainder column starting at the

decimal number 20? bottom.

¢¡¤£¦¥¨§ ©  §   §©¡¤©¡¡ 

Department of Computer Science, University College Cork 6 Department of Computer Science, University College Cork 7 CS1101: Systems Organisation Radix Numbers: Conversions CS1101: Systems Organisation Radix Numbers: Conversions Decimal-Binary Conversion by Halving Binary-Decimal Conversion

Quotients Remainders

Binary integers can also be converted to 1 4 9 2 decimal in two ways. One method consists of 7 4 6 0 summing up the powers of 2 corresponding to 3 7 3 0 the I bits in the number. 1 8 6 1

9 3 0 Example:

4 6 1

£

§  ¢ §©   §  2 3 0 ©¡¤©©¡ 1 1 1 5 1 2 1 1 0 0 1

1 0 1 1 1 0 1 0 1 0 0 = 149210

Figure A.5 Conversion of the decimal number 1492 to binary by successive halving.

Department of Computer Science, University College Cork 8 Department of Computer Science, University College Cork 9

CS1101: Systems Organisation Radix Numbers: Conversions CS1101: Systems Organisation Radix Numbers: Conversions Binary-Decimal Conversion by Doubling Binary-Decimal Conversion by Doubling

1 0 1 1 1 0 1 1 0 1 1 1

×

1 + 2 1499 = 2999 Result The binary number is written vertically, one bit 1 + 2 × 749 = 1499 per line, with the leftmost bit on the bottom. 1 + 2 × 374 = 749 0 + 2 × 187 = 374 1 + 2 × 93 = 187

1 + 2 × 46 = 93 The bottom line is called line 1, the one above 0 + 2 × 23 = 46 it line 2, and so on. 1 + 2 × 11 = 23 1 + 2 × 5 = 11 1 + 2 × 2 = 5 The decimal number will be built up in a parallel 0 + 2 × 1 = 2 1 + 2 × 0 = 1 Start here column next to the binary number.

Begin by writing a 1 on line 1. Figure A.6 Conversion of the binary number

101110110111 to decimal by successive doubling. ¡

The entry on line consists of two times the

© ¡ entry on line ¡£¢ plus the bit on line (either 0 or 1).

The entry on the top line is the answer.

Department of Computer Science, University College Cork 10 Department of Computer Science, University College Cork 11 CS1101: Systems Organisation Radix Numbers: Conversions Decimal-Octal & Decimal-Hexadecimal

Decimal-to-octal and decimal-to-hexadecimal conversion can be accomplished either by first converting to binary

Then convert to the desired system or convert by subtracting powers of 8 or 16.

Department of Computer Science, University College Cork 12