To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:



Similar documents
=

Base Conversion written by Cathy Saxton

Useful Number Systems

Computer Science 281 Binary and Hexadecimal Review

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

CSI 333 Lecture 1 Number Systems

The string of digits in the binary number system represents the quantity

Lecture 2. Binary and Hexadecimal Numbers

Lecture 8: Binary Multiplication & Division

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Number Representation

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Systems I: Computer Organization and Architecture

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Binary Adders: Half Adders and Full Adders

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Solution for Homework 2

Memory is implemented as an array of electronic switches

NUMBER SYSTEMS. William Stallings

CHAPTER 5 Round-off errors

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Section 1.4 Place Value Systems of Numeration in Other Bases

Binary, Hexadecimal, Octal, and BCD Numbers

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Counting in base 10, 2 and 16

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Binary Numbering Systems

NUMBER SYSTEMS. 1.1 Introduction

Numbering Systems. InThisAppendix...

5.1 Radical Notation and Rational Exponents

The BBP Algorithm for Pi

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Lecture 11: Number Systems

Binary Representation

Chapter 4: Computer Codes

Subnetting Examples. There are three types of subnetting examples I will show in this document:

CPEN Digital Logic Design Binary Systems

APPENDIX B. Routers route based on the network number. The router that delivers the data packet to the correct destination host uses the host ID.

The Hexadecimal Number System and Memory Addressing

An Efficient RNS to Binary Converter Using the Moduli Set {2n + 1, 2n, 2n 1}

CS201: Architecture and Assembly Language

Data Storage 3.1. Foundations of Computer Science Cengage Learning

DNA Data and Program Representation. Alexandre David

Session 29 Scientific Notation and Laws of Exponents. If you have ever taken a Chemistry class, you may have encountered the following numbers:

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

26 Integers: Multiplication, Division, and Order

COMPSCI 210. Binary Fractions. Agenda & Reading

Section 4.1 Rules of Exponents

1 Description of The Simpletron

Number and codes in digital systems

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

Chapter 2: Basics on computers and digital information coding. A.A Information Technology and Arts Organizations

Number Systems and Radix Conversion

Chapter 1: Digital Systems and Binary Numbers

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

23. RATIONAL EXPONENTS

Measures of Error: for exact x and approximation x Absolute error e = x x. Relative error r = (x x )/x.

BINARY CODED DECIMAL: B.C.D.

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS321. Introduction to Numerical Methods

2.6 Exponents and Order of Operations

Correctly Rounded Floating-point Binary-to-Decimal and Decimal-to-Binary Conversion Routines in Standard ML. By Prashanth Tilleti

Session 7 Fractions and Decimals

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

1.4 Compound Inequalities

THE BINARY NUMBER SYSTEM

3. Convert a number from one number system to another

Unsigned Conversions from Decimal or to Decimal and other Number Systems

HOMEWORK # 2 SOLUTIO

Numerical Matrix Analysis

Positional Numbering System

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Inequalities - Absolute Value Inequalities

Addition Methods. Methods Jottings Expanded Compact Examples = 15

Intel Hexadecimal Object File Format Specification Revision A, 1/6/88

2011, The McGraw-Hill Companies, Inc. Chapter 3

3.3 Addition and Subtraction of Rational Numbers

Trigonometric Functions and Triangles

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Adding and Subtracting Positive and Negative Numbers

Figure 1. A typical Laboratory Thermometer graduated in C.

MACHINE INSTRUCTIONS AND PROGRAMS

( 214 (represen ed by flipping) (magnitude = -42) = = -42 (flip the bits and add 1)

Properties of sequences Since a sequence is a special kind of function it has analogous properties to functions:

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

A Little Set Theory (Never Hurt Anybody)

Grade 6 Math Circles. Binary and Beyond

Transcription:

Binary Numbers In computer science we deal almost exclusively with binary numbers. it will be very helpful to memorize some binary constants and their decimal and English equivalents. By English equivalents we mean expressions such as 10 Megabytes, 45 Kilobytes, and 200Gigahertz (a byte is eight binary digits treated as a group to represent a single quantity. A byte of storage is typically required to store a single alphanumeric character.) 2 0 =1 =1 2 5 =32 =100000 2 1 =2 =10 2 6 =64 =1000000 2 2 =4 =100 2 7 =128 =10000000 2 3 =8 =1000 2 8 =256 =100000000 2 4 =16 =10000 2 9 =512 =1000000000 Large quantities are generally given in powers of two, but also have English names commonly associated with powers of ten. For instance, 1 Kilobyte is not 1000 (10 3 ) bytes, but is 2 10 (1024) bytes and is referred to as a kilobyte, or 1KB. In computer science, the phrase one thousand will usually mean 1024 10, not 1000 10. Similarly, a million is 2 20 and a billion is 2 30 ; 2 20 bytes is called a megabyte (1 MB), and 2 30 bytes is a gigabyte (1 GB).. 2 10 =1024=1K 3 2 20 =1M 2 30 =1G To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic: Examples: 2 a+b = 2 a x 2 b 2 a-b = 2 a /2 b 2 -a = 1/2 a 12. Converting binary exponential expressions to 'English'abbreviation 2 12 = 2 2 x 2 10 = 4 x 1K = 4K 3 Note, Kilobyte is denoted by KB (and Kilobit is denoted by Kb). The K, M, and G identify only the prefixes, since we may count other things besides bytes. NTC 1/23/05 21

2 33 = 2 3 x 2 30 = 8 x 1G = 8G 2 27 = 2 7 x 2 20 = 128 x 1M = 128M 13. Converting 'English'abbreviations to binary exponential expressions. 32G = 32 x 1G = 2 5 x 2 30 = 2 35 16K = 16 x 1K = 2 4 x 2 10 = 2 14 512M = 512 x 1M = 2 9 x 2 20 = 2 29 Practice Problems - Binary-to-English Conversions 1. Convert the following binary exponential expressions to their 'English' counterparts: 2 3 2 13 2 23 2 33 2 27 2 15 2 31 2 20 2 38 2. Convert the following 'English'expressions to their binary exponential equivalents: 3. Word problems 1K 256 16M 32K 128G 2M 4G 512K 8M a. How much memory is supported by a system with 24-bit memory addresses? b. How large an address is needed to address a memory of 4GB? c. How many op code bits are need in an instruction which supports 128 different operations? d. How many registers are supported by an instruction with a register ID field of 6 bits? Binary Number Conversion We will frequently have occasion to convert binary numbers to decimal numbers. The techniques previously described for number systems in general are fully applicable here. For example, converting binary to decimal: 1011011 2 = 1x2 6 + 0x2 5 + 1x2 4 + 1x2 3 + 0x2 2 + 1x2 1 + 1x2 0 NTC 1/23/05 22

= 64+0+16+8+0+2+1 = 91 10.101 2 =1x2-1 + 0x2-2 + 1x2-3 = 1/2 1 + 0/2 2 + 1/2 3 = 1/2 + 0/4 + 1/8 = (4+1)/8 = 5/8 =.625 10 For the interested student, note that there are ways to speed up the conversion of integer binary numbers to decimal. They rely on two things. First that you memorized the decimal/binary equivalents of the numbers 0 thru 15, as shown in table TN1; second, remember that shifting a binary number one position to the left (inserting a zero in the rightmost postion) multiplies that number by 2 (just as shifting a decimal number to the left multiplies it by 10). Then 1011011 could be rapidly converted as follows: 1011011 2 = 1011000 + 011 = 11 x 8 + 3 since the first 4 digits (1011 = 11) are shifted 3 places (=8) = 91 10 or, equivalently, 1011011 2 = 1010000 + 1011 = 10 x 8 + 11 = 91 10 Examples of converting decimal to binary: 91 10 to binary - 91/2 = 45 with a remainder of 1 45/2 = 22 with a remainder of 1 22/2 = 11 with a remainder of 0 11/2 = 5 with a remainder of 1 5/2 = 2 with a remainder of 1 2/2 = 1 with a remainder of 0 1/2 = 0 with a remainder of 1 giving 1011011 2.75 10 to binary -.75 x 2 = 1.50 first digit is 1.50 x 2 = 1.00 second digit is 1.00 x 2 = 0 we are done giving.11 2 NTC 1/23/05 23

Alternatively, some students find it faster to use the following scheme for converting decimal integer numbers to binary. Simply find the largest power of 2 which is smaller than or equal to the given number (you should have memorized the first 10 powers of 2), and subtract this power of two from the number. Continue subtracting powers of two from the result of the previous subtraction until the result is zero. The powers of 2 which were able to be subtracted are then the positions of all the 1's in the result. Example, 91 10 91-64 = 27 Therefore 64 = 2 6 is the position of the first 1 27-16 = 11 Therefore 16 = 2 4 is the position of the second 1 11-8 = 3 8 = 2 3 is the third position 3-2 = 1 2 = 2 1 is the fourth position 1-1 = 0 1 =2 0 is the fifth 1 in the answer giving 1011011 2 Negative Binary Numbers In what follows, keep in mind that, since we are interested in using these numbers within the hardware of a computer, there is a finite amount (usually small) of space (storage) allocated for each number. In the computer s memory this may be referred to as a word. In the CPU such data is held in storage devices called registers. Each register has a finite size, measured in bits, where each bit is a binary digit (in fact, the word bit is a contraction of the words binary digit. We assume that every binary number uses all the bits available in the specified register. A group of adjacent 8 bits in a register is called a byte. A 32-bit register, therefore, is also a 4-byte register. There are six common ways of representing a number in binary: Unsigned Sign and Magnitude 2's complement Biased Binary Coded Decimal Floating Point The second, third and fourth are binary representations which allow for the representation of negative as well as positive numbers. 5. Unsigned binary numbers. All the bits in the register are weighted per there bit positions, and are numbered 0 through n-1 from right to left. If the register is n NTC 1/23/05 24

bits wide, it can hold 2 n different values, in the range (assuming integers only) of 0 through +2 n - 1. For example, a 16-bit register holds 2 16 = 65536 values, 0 thru 65545. Note that all arithmetic results which end up in an n-bit register can never have a value greater than 2 n - 1, and is thus effectively arithmetic modulo 2 n. Keep this in mind for future discussion. 6. Sign and Magnitude. In this convention, only bits 0 through n-2 are used to hold the number, bit n-1 (the most significant bit) is used to specify the sign of the number. A zero in position n-1 means that the number is positive, a one in position n-1 means that the number is negative. In an eight bit register, for example, bit 7 represents the sign. The number 00111100 2 is the number + 60 10 (the same as if the number were unsigned), and 10111100 2 is - 60 10 (this would be 188 10 if it were an unsigned number). The range of values able to be represented in this form is -(2 n-1-1) through +2 n-1-1. The values in a 16 bit register are -32767 thru +32767. One problem with this convention is that 0 10 has two representations: 0000000 2 and 10000000 2. 7. 2's complement. Both 2's complement and 1's complement may be used to incorporate both positive and negative number representations in a computer; we will focus on 2's complement for reasons that will become clear later. As in the previously mentioned conventions, positive numbers (and zero) are represented normally, just as if they were unsigned binary numbers. However, the negative of a number in 2's complement form is simply the value you get when you subtract the positive representation of the number from 2 n. That is, if x is binary n-bit number, then -x = 2 n - x [N10] There is a simple way to perform this operation without actually having to do subtraction, consisting of a two step process as follows: i. Complement the value. That is, change every 1 to a 0 and every 0 to a 1. ii. Add 1 to the result of the first step. For example, in an eight bit register, the number +56 10 is given by 00111000 2. Complementing this gives 11000111 2. Adding 1 gives 11001000 2 which is used to represent -56 10. Recall that when converting a binary number to a decimal number, we simply add up all the weighted values of the positions in the binary where there is a 1. To convert a 2's complement number, the same procedure is applied, but the weight NTC 1/23/05 25

of the (n-1) st bit (the most significant bit) has a weight of -2 n-1 instead of +2 n-1!. Thus the decimal value of the four-bit 2's complement number 1110 is -2 3 + 2 2 + 2 1 = -8 + 4 + 2 = -2. The range of values able to be represented in 2's complement is -2 n-1 through +2 n- 1-1. This is one more negative value than is represented by the sign & magnitude representation, corresponding to the fact that there is only one representation for zero. Note 1: When this convention is being used, all numbers are considered 2's complement numbers, regardless of whether they are positive or negative. Don t fall into the trap of thinking that only the negative numbers are in 2's complement. Note 2: Negative numbers can be converted to positive numbers using the exact same procedure: complement and add 1 (i.e. you don t have to do the operations in reverse by subtracting 1 and then complementing). Note 3: Negative numbers will always have a leading 1, positive numbers will always have a leading 0. If you are asked to convert a positive decimal number to a 2's complement number, don t forget to add the leading 0! Note 4: When a positive n-bit number and its negative representation (in 2's complement form) are added together, the result is 2 n (see N10]). Since the maximum value an n-bit register can hold is 2 n -1, the result is all zeros in the register and an overflow (This is an addition modulo 2 n ). We ignore the overflow and get the correct result: zero. As we will see this is important for implementing binary arithmetic. Note 5: A 2's complement integer n bits in length can be represented similarly to [N8] except that the weight of the most significant bit is negative. N = (- d n-1 x -2 n-1 ) + d i 2 i [N11] Radix Conversion of 2's complement numbers. Decimal to 2's Complement Conversion Converting a positive decimal number to 2's complement form is identical to converting it to an unsigned binary number, except that you must be sure to include a leading zero. Example 14: Represent +19 10 in 2's complement notation NTC 1/23/05 26

+19 10 = 010011 2, not 10011 2 (which would be - 13) To convert a negative decimal number to 2's complement form a. Convert the number to binary, assuming it is positive, as above; b. Use the complement-and-add procedure to convert the positive 2's complement number into a negative 2's complement number. Example 15: Represent -19 10 in 2's complement notation. a. +19 = 010011 (remember to include the leading 0!) b. complement and add: 101100 + 1 = 101101 2 2's Complement to Decimal Conversion To convert a binary 2's complement number (regardless of sign) to decimal, use [N11]. That is, sum up the weights of all the positions containing a one, except that the weight of the most significant bit is negative. Example: 101101 = -2 5 + 2 3 + 2 2 + 2 0 = -32 + 8 + 4 + 1 = -32 + 13 = - 19 10 Value of bit n-1 Value of remainin g bits Example : +19 Unsigned d n-1 2 n-1 d i r i 010011 n/a Sign & Magnitude Example : -19 sign d i r i 010011 110011 2's Complement -d n-1 2 n-1 d i r i 010011 101101 Table TN2. Table TN2 summarizes these number formats and includes the previous examples. Notice that these formats differ only in the interpretation of the most significant bit position (2 n-1 ), and that positive numbers have the same representation regardless of format (although the leading 0 is not required if the number is known to be in unsigned format.) NTC 1/23/05 27

Practice problems - 2's Complement Numbers: 1. Given the binary number 11011011, what is its decimal value if it is a a. unsigned binary number (Ans: 219) b. sign and complement number (Ans: -91) c. 2's complement number (Ans: -37) 2. Convert +24 into a 2's complement number (Ans: 011000 Note the leading zero!) 3. Convert -24 into a 2's complement number (Ans: 1010000) 4. Convert -1 into a 2's complement number (assume a 4-bit result) (Ans: 1111) Note that -1 is 11111... 2 regardless of the size of the register. Note also that any positive or negative 2's complement number can have its leading digit propagated to the left without changing the number. 5. What is the minimum (maximum negative) number which can be represented using a 6-bit 2's Complement representation? What is the maximum (positive) number? 6. What is the minimum (maximum negative) number which can be represented using a n-bit 2's Complement representation? What is the maximum (positive) number? Convert, if possible, the following decimal numbers to 2's complement assuming an 8-bit binary representation for all. 8. 1 11. -1 14. 72 9. 127 12. -127 15. -105 10. 255 13-255 16. -6 Convert the following 2's complement numbers to decimal. 17. 1111 20. 11110000 18. 001101 21. 11111111 19. 0000000 22. 01 NTC 1/23/05 28

Biased numbers - It is sometimes desirable (we ll see an example in a bit) to have the range of negative and positive numbers by monotonically increasing - that is, each successive number is determined by adding one to the previous number. Basically, we take our numerical sequence, and shift the meanings of the combinations of ones and zero so that some of the combinations are negative in interpretation. For example, using three bit binary numbers: binary unsigned Bias(3) 000 0-3 001 1-2 010 2-1 011 3 0 100 4 1 101 5 2 110 6 3 111 7 4 Notice that the decimal values have been shifted down (or biased by) 3 positions, eliminating the values 5 thru 7 and inserting the values -3 thru -1. The value of the bias is usually chosen to be roughly one half of the total range of numbers available, which is determined by the number of bits allocated to the number. In the above example, three bits give a total of eight numbers, 0 thru 7; we would generally choose a bias of three or four, depending on whether we wanted more positive or negative numbers. In general, for the purposes of this text, choose a bias as follows: 1. Given the maximum number of binary bits, n, choose a bias 4 = 2 n /2 = 2 n-1 If n = 8, the bias should be 128 ( = 2 8 /2 = 256/2 = 128 = 2 8-1 = 2 7 If n = 16, the bias should be 2 16 /2 = 32K 2. Given a negative decimal number, assume a bias equal to the minimum power 4 In practice, the bias is general chosen to be 1 less then the values presented here. For instance, as we will see, the IEEE single-precision floating point specification, which uses a biased 8 bit number for the exponent representation uses a bias of 127, not 128. NTC 1/23/05 29

of two greater than the absolute value of the negative decimal number. Also, if 2m is the bias, then the number of bits in the biased representation must be m+1. This is because, if the bias is 2 m, then the range of numbers to be represented is - 2 m -1 thru +2 m for a total range of 2 x 2 m = 2 m+1. This requires m+1 bits in the binary biased representation. If the negative decimal number is -23 assume a bias equal to 32. The number of bits in the biased representation will be 6. If the negative decimal number is -129 assume a bias equal to 256. The number of bits in the biased representation will be 9. Converting Decimal numbers to Biased binary numbers Examples: 1. Add the bias to the decimal number 2. convert the resulting decimal number to binary 16. Assume a five-bit biased binary representation. What bias would you use? (Ans. 15 or 16, usually 15). 17. Using this bias, convert the decimal numbers 0, 6, and -12 to biased format. Ans: 0 + 15 = 15 = 01111 2 6 + 15 = 21 = 10101 2-12 + 15 = 3 = 00011 2 18. Convert -49 to biased binary representation. Choose a bias equal to 2 m such that 2 m > 49. In this case, the bias = 64. Then a. Add the bias: -49+64 = 15 b. Convert the result to binary: 15 10 = 0001111 2 Notice that, since 2 m = 64, m = 6 and the binary representation must contain m+1 bit positions (7 in this case). Converting biased binary numbers to decimal 1. Convert the biased binary number to decimal NTC 1/23/05 30

Examples: 2. Subtract the bias from the resulting decimal number 19. Convert the biased binary number 111111 to decimal Since there are 6 binary digits, assume the bias is 2 6 /2 = 2 5 =32 1. Convert to decimal: 111111 2 = 63 10 2. Subtract the bias: 63-32 = 31. 20. Convert the biased binary number 00101011 to decimal. Since there are 8 bits, assume a bias of 128 1. 00101011 2 = 43 10 2. 43-128 = -85 21. Convert 0001 to decimal. Bias = 8 0001 = 1 0-8 = -7 10 NTC 1/23/05 31

Practice Problems - Biased Binary Numbers 1. What bias should you choose for biased binary representations with each of the following number of bits? a. 8 bits b. 2 bits c. 24 bitsd. 13 bits 2. What bias should you choose for biased binary representation of each of the following decimal numbers? a. -10 b. -1013 c. -342 d. +75 3. Convert the following decimal numbers to the appropriate biased binary format. a. -34 b. -12 c. +245 d. -129 4. Convert the following biased binary numbers to decimal a. 101 b. 00011 c. 111111 d. 0000 Table TN3 shows the decimal values associated with the binary numbers 0000 through 1111 in each of the binary number representations discussed. NTC 1/23/05 32

UNSIGNED BINARY UNSIGNED DECIMAL SIGN & MAGNI- TUDE 2'S COMPL- EMENT BIAS(8) BIAS(7) 0000 0 0 0-8 -7 0001 1 1 1-7 -6 0010 2 2 2-6 -5 0011 3 3 3-5 -4 0100 4 4 4-4 -3 0101 5 5 5-3 -2 0110 6 6 6-2 -1 0111 7 7 7-1 0 1000 8-0 -8 0 1 1001 9-1 -7 1 2 1010 10-2 -6 2 3 1011 11-3 -5 3 4 1100 12-4 -4 4 5 1101 13-5 -3 5 6 1110 14-6 -2 6 7 1111 15-7 -1 7 8 Table TN3. Note: The above representations of binary numbers seem to refer to only integers. However, a binary point (as opposed to a decimal point) could be assumed to be anywhere in the register holding the number. If we have an eight bit (1 byte) register containing the bits 10100111, it might, depending on the application, be interpreted to contain 10100111 2 = 167 10, or 1010011.1 2 = 83.5 10, or 1010.0111 2 = 10.4375 10 These are called Fixed Point numbers, in contrast to floating point numbers which will be discussed shortly. Note that shifting a binary number left (and inserting a zero into the least significant digit) is the same as multiplying by 2. Compare this to adding a low order zero to a decimal number. Shifting right performs the DIV 2 operation, since the least significant NTC 1/23/05 33

bits are shifted out of the register and are lost. 011011 2 = 27 10 110110 2 = 54 10 011011 2 = 27 10 NTC 1/23/05 34