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



Similar documents
Computer Science 281 Binary and Hexadecimal Review

CSI 333 Lecture 1 Number Systems

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

NUMBER SYSTEMS. William Stallings

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

Solution for Homework 2

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

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

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

Numbering Systems. InThisAppendix...

Base Conversion written by Cathy Saxton

CHAPTER 5 Round-off errors

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

HOMEWORK # 2 SOLUTIO

Useful Number Systems

Number Representation

=

Lecture 2. Binary and Hexadecimal Numbers

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

Normal distribution. ) 2 /2σ. 2π σ

26 Integers: Multiplication, Division, and Order

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

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

Review of Scientific Notation and Significant Figures

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

Binary Numbers. Binary Octal Hexadecimal

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

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Lecture 11: Number Systems

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

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

Chapter 4: Computer Codes

3. Mathematical Induction

Binary Representation

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

COMPSCI 210. Binary Fractions. Agenda & Reading

Math 319 Problem Set #3 Solution 21 February 2002

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

Binary Adders: Half Adders and Full Adders

Systems I: Computer Organization and Architecture

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

Section 1.4 Place Value Systems of Numeration in Other Bases

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Student Outcomes. Lesson Notes. Classwork. Discussion (10 minutes)

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

MATH 4330/5330, Fourier Analysis Section 11, The Discrete Fourier Transform

Grade 6 Math Circles. Binary and Beyond

CPEN Digital Logic Design Binary Systems

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

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

CS321. Introduction to Numerical Methods

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

Handout #1: Mathematical Reasoning

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

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

2.6 Exponents and Order of Operations

3. Convert a number from one number system to another

Scientific Notation. Section 7-1 Part 2

Chapter 3. if 2 a i then location: = i. Page 40

Session 7 Fractions and Decimals

Positional Numbering System

5.1 Radical Notation and Rational Exponents

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

Math Workshop October 2010 Fractions and Repeating Decimals

25 Integers: Addition and Subtraction

The Answer to the 14 Most Frequently Asked Modbus Questions

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Stanford Math Circle: Sunday, May 9, 2010 Square-Triangular Numbers, Pell s Equation, and Continued Fractions

8 Primes and Modular Arithmetic

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

CONTENTS 1. Peter Kahn. Spring 2007

Basics of Counting. The product rule. Product rule example. 22C:19, Chapter 6 Hantao Zhang. Sample question. Total is 18 * 325 = 5850

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

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Basic Proof Techniques

1 Definition of a Turing machine

The BBP Algorithm for Pi

5544 = = = Now we have to find a divisor of 693. We can try 3, and 693 = 3 231,and we keep dividing by 3 to get: 1

Binary, Hexadecimal, Octal, and BCD Numbers

NUMBER SYSTEMS. 1.1 Introduction

Just the Factors, Ma am

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

CS 103X: Discrete Structures Homework Assignment 3 Solutions

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

CS201: Architecture and Assembly Language

DNA Data and Program Representation. Alexandre David

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Cartesian Products and Relations

Domain of a Composition

Lecture Notes on Linear Search

Mathematical Induction

MATH10040 Chapter 2: Prime and relatively prime numbers

Cardinality. The set of all finite strings over the alphabet of lowercase letters is countable. The set of real numbers R is an uncountable set.

Indices and Surds. The Laws on Indices. 1. Multiplication: Mgr. ubomíra Tomková

Numerical Matrix Analysis

Mathematical goals. Starting points. Materials required. Time needed

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

Sample Induction Proofs

Lecture 16 : Relations and Functions DRAFT

FAST INVERSE SQUARE ROOT

Transcription:

Exercises 1 - number representations Questions 1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal: (a) 3012 (b) - 435 2. For each of the following bit strings: Which decimal number is represented, assuming the bit string is a signed integer? Which decimal number is represented, assuming the bit string is an unsigned integer? Convert the bit string to hexadecimal. (a) 0100100101 (b) 1111101001 3. Convert the following two decimal numbers to signed binary (twos complement). Be careful to choose enough bits. (a) 200 (b) 259 4. Convert 0xff3e from hexadecimal to binary, treat this binary number as a signed integer, and convert it to decimal. 5. (a) Convert the decimal number -499 to signed binary (twos complement). (b) Convert 0xfc to binary, treat it as a signed integer, and convert to decimal. 6. (more challenging) Writing a positive integer in binary means expressing it as a sum of powers of 2: (b n 1 b n 2... b 2 b 1 b 0 ) 2 This representation makes sense for unsigned numbers. But what about signed numbers? How can we represent a signed number as a sum of powers of 2? Which powers of two are represented and what are the b i? 7. Convert -5.75 to binary. n 1 i=0 b i 2 i last updated: 19 th Jan, 2016 1

Exercises 1 - number representations 8. Represent the following two decimal numbers as single precision float. Show: a scientific notation representation the significand, exponent, and sign bits (32 bits total) a hexadecimal representation of these 32 bits (a) 15.0625 (b) 35.6875 (c) 86.5625 9. What are the largest and smallest positive, finite, normalized numbers that can be represented as IEEE single precision float? It is sufficient for you to write the answer using scientific notation. 10. Consider representing the set of consecutive numbers using: (a) 32 bit unsigned (b) 32 bit signed { 1, 2, 3, 4,..., n } (c) IEEE single precision floating point (a bit tricky) For each of these representations, what is the largest n such that every number in the above set can be represented, that is, no gaps between numbers? write this largest n using hexadecimal (rather than binary) 11. (a) Approximate the decimal number 43.00008 as a normalized binary number in scientific notation, with eight bits of significand. (b) Convert the decimal number 0.125 into IEEE single precision floating point. Give your answer in hexadecimal. 12. (challenging, you need to consider various cases) (a) Consider two normalized floating point numbers f1 and f2 that are represented as IEEE single precision floats. This defines two 32-bit strings. Suppose f1 is less than f2, that is, f1 < f2. If the same two 32-bit strings were treated as unsigned integers (call them i1 and i2 respectively), can we conclude that i1 < i2? last updated: 19 th Jan, 2016 2

Exercises 1 - number representations (b) Similar to (a), if the two 32 bit strings were treated as signed integers j1 and j2, could we conclude that j1 < j2? 13. The IEEE single precision floating point standard allows us to represent less than 2 32 different numbers. Of these numbers: (a) How many are strictly between 2 5 and 2 4? (b) How many are strictly between 2 13 and 2 14? (c) How many are strictly between 2 47 and 2 48? 14. (a) Write the following binary number as an IEEE single precision float: 0.10101010..., that is, bits 10 repeating infinitely many times to the right of the binary point. (b) Convert 0.10101010... from binary to decimal. (cute) last updated: 19 th Jan, 2016 3

Solutions 1. (a) The signed (twos complement) representation of 3012 is 0000 1011 1100 0100. In hexadecimal, this is 0x0bc4. (b) The signed representation of 435 is 0000000110110011. Thus, the signed representation of -435 is 1111111001001101 In hexadecimal, the latter is 0xfe4d. 2. (a) if signed then 293 if unsigned then (also) 293 Convert to hex: 0x125 (b) if signed then -23 if unsigned then 1001 ( one thousand and one ) Convert to hex: 0x3e9 3. (a) To convert 200 to binary, we convert 200 to binary, invert the bits, and add 1. 200 in binary is... 0011001000. For this to be a signed number, the most significant bit must be 0 since 200 is positive. Let s use ten bits: 0011001000. Inverting the bits yields 1100110111. Adding 1 yields the answer: 1100111000. (b) To convert 259 is easier. The answer is 0100000011. 4. Converting the hexadecimal number 0xff3e to signed binary, gives 1111 1111 0011 1110. This is a negative number since the most significant bit is 1. To determine what this number is, we take its negative by inverting the bits and adding 1, yielding 0000000011000010. This is 2 + 2 6 + 2 7 which is 2 + 64 + 128 = 194. Thus, the answer is 194. 5. (a) (499) 10 = (0001 1111 0011) 2. Inverting the bits, we obtain 1110 0000 1100. Then add 1 to obtain the answer 1110 0000 1101. This is in 2 s complement with 12 bits. You needed at least one leading bit, e.g. 10 0000 1101 is accepted. (b) 0xFC in binary is 1111 1100. This is a negative number, so we invert the bits and add 1 to obtain (0011) 2 + 1 = (0100) 2 = (4) 10 so the answer is 4. 6. For a signed number, we have For example, take n = 8. (b n 1 b n 2... b 2 b 1 b 0 ) 2 b n 1 2 n 1 + (10000000) 2 = 128 = 2 7, (10000001) 2 = 127 = 2 7 + 2 0, (10000010) 2 = 126 = 2 7 + 2 1,... n 2 i=0 b i 2 i. That is, the 1 in the highest order bit defines the smallest (most negative) number represented and the lower order other bits just add positive values bringing the number closer to 0. last updated: 19 th Jan, 2016 4

7. Note that - 5.75-5 +.75. Rather 5.75 = -(5.75). In the former case, you might have converted -5 to binary by saying (5) 10 = (101) 2 = (0101) 2 and then flipping the bits and adding 1 to get (1011) 2. You then might have converted.75 to binary as (.11) 2 and concluded the answer is (1011.11) which would be incorrect. The correct way to go is to write 5.75 in binary as 0101.11, where we have added a high order 0 bit so that the number is positive when interpreted as a signed number. We then compute its negative by inverting the bits 1010.00 and adding.01 to get the answer 1010.01. Verify that adding this to 5.75 gives 0. 8. (a) Converting 15.0625 to binary gives 1111.0001 or 1.1110001 2 3 in scientific notation. Since the exponent is 3, the exponent code e is determined by e 127 = 3, and so the exponent code is the (unsigned) binary representation of 130 which is 10000010. The sign bit is 0 since the number is positive. Writing as a string (sign bit, exponent, significand) gives which in hexadecimal is: 0x41710000 0 10000010 11100010000000000000000 (b) Converting 35.6875 to binary is 100011.1011 which is 1.000111011 2 5 The sign bit is 1. The exponent code is the unsigned representation of 132 (since e 127 = 5). Thus the exponent code is 10000100. The significand is 00011101100000000000000. Writing as a string (sign bit, exponent, significand) gives which in hexadecimal is: 0xc20ec000. (c) 86.5625 = 1.0101 1010 0100 2 6 1 10000100 00011101100000000000000 e = 127 + 6 = 133. So we have 0 10000101 01011010010000000000000. Re-group the bits to obtain: (0100 0010 1010 1101 0010 0000 0000 0000) 2 = 0x42AD2000 9. The largest exponent code is 11111110 i.e. 254 (since 11111111 is reserved). Thus, the largest exponent that can be represented is 254 127 = 127. Thus, the largest (finite) float is 1.1111111111111111111111111 2 127. The smallest exponent code for normalized numbers is 00000001 (since 00000000 is reserved). This exponent code has value 1 since it is treated as an unsigned number. Thus, the smallest exponent that can be represented is 1 127 = 126. It follows that smallest (normalized positive) float is 1.00000000000000000000000 2 126 which is just 2 126. 10. (a) The largest unsigned is 2 32 1 which in hex is 0xffffffff. (b) The largest signed is 2 31 1 which in hex is 0x7fffffff. last updated: 19 th Jan, 2016 5

(c) First, note that 1 to 2 24 1 can all be represented exactly. Why? Because when you write out one of the those numbers in binary (as unsigned), you only need 24 bits. When you try to write any of these numbers in IEEE format, the most significant 1 bit isn t used because we are representing the numbers as normalized. So we have room for up to 23 bits, in the significand and we re ok. In particular, the number 111111111111111111111111 which has 24 1 s can be represented exactly. This number is 2 24 1. Next, what about 2 24 = (1000000000000000000000000) 2? Can it be written exactly? Yes, because it is just 1.0 2 24 and I hope by now it is obvious that this can be written exactly in IEEE format, namely the exponent value is 24 and the significand is all 0 s. So, all the numbers from 1 to 2 24 can be represented exactly. What about 2 24 + 1 = (1000000000000000000000001) 2 = (1.000000000000000000000001) 2 2 24? Can it be represented exactly? No it can t. Why not? Because there is only room for 23 bits in the significand and those bits would be the 23 0 s to the right of the binary point, and so we wouldn t be able to get in the 2 24 bit. Thus, the largest n is such that 1,..., n can be represented exactly in single precision IEEE format is 2 24. 11. (a) To approximate 43.00008 as a normalized binary number in scientific notation, we first convert the part to the left of the decimal point to binary. Converting 43 to binary is 101011. This is already close to the number of bits of significand that we need. At this time, you should suspect that the 0.00008 will not play a role in your answer. If you wish to verify this, you can try: 101011.(00008) 10 = 1010110.(00016) 10 2 1 = 10101100.(00032) 10 2 2 = 101011000.(00064) 10 2 3 but at this point we have our eight bits of significand. So, This is our answer. 43.00008 1.01011000 2 8 2 3 = 1.01011000 2 5 (b) First write the decimal number 0.125 in normalized scientific notation: 1.0 2 3. The significant is thus all 0 s. To code the exponent 3, we need x 127 = 3. Thus we need the 8-bit binary of x = 124, which is 01111100. The sign bit is 0 (positive number), and the IEEE encoding is the last updated: 19 th Jan, 2016 6 (0 01111100 00000000000000000000000).

To convert to hexadecimal, we group into 4-tuples: and we get 0x3e000000. (0011, 1110, 0000, 0000, 0000, 0000, 0000, 0000) 12. (a) No. As a counterexample, suppose that the most significant bit of f1 is 1 and the most significant bit of f2 is 0. Then f1 < f2 since the most significant bit is the sign bit: 1 is negative and 0 is positive. Treated as unsigned numbers (i1 and i2), however, the MSB s alone would imply that i1 > i2. Another way to see this is to consider the ordered list of all 2 32 possible words. 0000... 0000 0000... 0001 0000... 0010 : 0111... 1111 1000... 0000 1000... 0001 : 1111... 1111 In the first half of these (MSB = 0), both the float and the int representing are increasing as we proceed down the list. Within the second half of these (MSB = 1), the float values are decreasing as we proceed down the list because the sign is negative and either the exponent or significand is decreasing. However the int values are increasing as we proceed down the list. Thus, within the second half, the ordering of the float values is opposite to the ordering of the int values. (b) No. Assume f1 < f2 and consider whether this implies j1 < j2. There are three cases. Only in the second case is j1 < j2. Note that once one of the cases fails, the proof is done, you have found a counterexample to the statement f1 < f2 implies j1 < j2. Case 1: the MSB s of both 32-bit numbers are 0 and so the float values are both positive. There can be two ways in which f1 < f2. The first is that the exponent of f1 is less than the exponent of f2. The second is if the exponent of f1 is equal to the exponent of f2, but the significand of f1 is less than that of f2. In either case, f1 < f2 implies that j1 < j2. (If you cannot see why, please see me or send me email.) Case 2: the MSB s of both 32-bit numbers are 1. In this case, the float values are both negative (and the integers j1 and j2 are both negative). Since we are assuming f1 < f2 and both are negative, it follows that the absolute value of f1 must be greater than the absolute value of f2. From this, it follows that either the exponent of f1 is greater than the exponent of f2, or the exponent of f1 is equal to the exponent of f2 but the significand of f1 is greater than that of f2. In either case, it follows that j1 > j2. To see this, recall the argument from case 1, but notice that both ints are on the negative part of the circle. last updated: 19 th Jan, 2016 7

Case 3: The MSB of one of f1 or f2 is 1 and the MSB of the other is 0. If f1 < f2, then there is only one for this to happen, namely that the MSB of f1 is 1 (f1 is negative) and the MSB of f2 is 0 (f2 is positive). In this case, if the numbers are interpreted as signed integers, then j1 would be negative (since MSB is 1) and j2 would be positive (since MSB is 0), and hence j1 < j2. 13. For all three questions, the answer is 2 23 1 different floating point numbers. For example, between 2 5 and 2 4 there are all the floating point numbers with 127 5 = 122 = (0111 1010) 2 as the exponent and 1 as the sign bit. Since there are 2 23 bits in the significand, and any setting of these produces a valid FPN, there are 2 23 different FPNs. We exclude the number with significand all 0 s, since this is the value 1.0 2 5. Thus the solution 2 23 1. last updated: 19 th Jan, 2016 8

14. (a) 0.101010 = 1.0101010101 2 1 So, the sign bit is 0, the exponent code is 126 or (01111110) two, and the 23 bit significand is 010 1010 1010 1010 1010 1010. So the 32 bits in the float and their corresponding hex representation are 0011 1111 0010 1010 1010 1010 1010 1010 or 0x3f2aaaaa (b) There are different ways to crank out the answer. Here is one:.101010 = 2 2 2 + 2 2 4 + 2 2 6 + 2 2 8 +... = 2 2 2 (1 + 2 2 + 2 4 + 2 6 + 2 8 + ) = 1 2 (1 + a + a2 + a 3 + a 4 + )where a = 2 2 = 1 1, i.e.geometric series 2 (1 a) = 1 2 (4 3 ) = 2 3 last updated: 19 th Jan, 2016 9