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



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

NUMBER SYSTEMS. William Stallings

=

Computer Science 281 Binary and Hexadecimal Review

CSI 333 Lecture 1 Number Systems

Binary Adders: Half Adders and Full Adders

Lecture 2. Binary and Hexadecimal Numbers

Useful Number Systems

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

Session 7 Fractions and Decimals

THE BINARY NUMBER SYSTEM

6.4 Normal Distribution

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.

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

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

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

Number Systems and Radix Conversion

Solutions of Linear Equations in One Variable

Base Conversion written by Cathy Saxton

Fractions to decimals

Pre-Algebra Lecture 6

Activity 1: Using base ten blocks to model operations on decimals

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

3. Convert a number from one number system to another

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

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

1. The Fly In The Ointment

Addition Methods. Methods Jottings Expanded Compact Examples = 15

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

5.4 Solving Percent Problems Using the Percent Equation

NUMBER SYSTEMS. 1.1 Introduction

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

1 Description of The Simpletron

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

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

Positional Numbering System

The Graphical Method: An Example

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

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

Lecture 11: Number Systems

23. RATIONAL EXPONENTS

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

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

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

CS321. Introduction to Numerical Methods

Basic Proof Techniques

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Lecture 13 - Basic Number Theory.

Chapter 7 Lab - Decimal, Binary, Octal, Hexadecimal Numbering Systems

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

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

Number Representation

Continued Fractions and the Euclidean Algorithm

Preliminary Mathematics

Decimal Notations for Fractions Number and Operations Fractions /4.NF

Lecture 3: Finding integer solutions to systems of linear equations

Mathematical Induction

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

Grade 6 Math Circles. Binary and Beyond

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

8 Divisibility and prime numbers

Every Positive Integer is the Sum of Four Squares! (and other exciting problems)

Solution to Exercise 2.2. Both m and n are divisible by d, som = dk and n = dk. Thus m ± n = dk ± dk = d(k ± k ),som + n and m n are divisible by d.

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

26 Integers: Multiplication, Division, and Order

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

Binary, Hexadecimal, Octal, and BCD Numbers

Math Review. Numbers. Place Value. Rounding Whole Numbers. Place value thousands hundreds tens ones

CS201: Architecture and Assembly Language

Chapter 11 Number Theory

3 Some Integer Functions

Multiplication and Division with Rational Numbers

8 Primes and Modular Arithmetic

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

Paramedic Program Pre-Admission Mathematics Test Study Guide

JobTestPrep's Numeracy Review Decimals & Percentages

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

2 Number Systems. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

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

3 Monomial orders and the division algorithm

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

COMPSCI 210. Binary Fractions. Agenda & Reading

Conversions between percents, decimals, and fractions

Chapter 2. Binary Values and Number Systems

Representation of functions as power series

Clock Arithmetic and Modular Systems Clock Arithmetic The introduction to Chapter 4 described a mathematical system

3.3 Addition and Subtraction of Rational Numbers

Partial Fractions. p(x) q(x)

Arkansas Tech University MATH 4033: Elementary Modern Algebra Dr. Marcel B. Finan

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

Section IV.1: Recursive Algorithms and Recursion Trees

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

Playing with Numbers

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

Lecture 8: Binary Multiplication & Division

Linear Programming. March 14, 2014

Handout #1: Mathematical Reasoning

Transcription:

Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about the number ten. Computers don t represent numbers using decimal. Instead, they represent numbers using binary, or base 2. Let s make sure we understand what binary representations of numbers are. We ll start with positive integers. In decimal, we write numbers using digits {0, 1,..., 9}, in particular, as sums of powers of ten. For example, (238) 10 = 2 10 2 + 3 10 1 + 8 10 0 In binary, we represent numbers using bits {0, 1}, in particular, as a sum of powers of two: (11010) 2 = 1 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 I have put little subscripts (10 and 2) to indicate that we are using a particular representation (decimal or binary). We don t need to always put this subscript in, but sometimes it helps. Let s consider how to count in binary. You should verify that the binary representation is a sum of powers of 2 that indeed corresponds to the decimal representation on the left. decimal binary ------- ------ 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 etc Just as we did last lecture with other bases, let s add two numbers which are written in binary: 11010 26 + 1111 15 ----- -- 101001 41 Make sure you see how this is done, namely how the carries work. For example, in column 0, we add 0 + 1 to get 1 and there is no carry. In column 1, we add 1 + 1 (in fact, 1 2 1 + 1 2 1 ) and we get 2 2 1 = 2 2 and so we carry a 1 over column 2. Do not proceed further until you understand this. 1

Converting from decimal to binary It is trivial to convert a number from a binary representation to a decimal representation. You just need to know the decimal representation of the various powers of 2. 2 0 = 1, 2 1 = 2, 2 2 = 4, 2 3 = 8, 2 4 = 16, 2 5 = 32, 2 6 = 64, 2 7 = 128, 2 8 = 256, 2 9 = 512, 2 10 = 1024,... Then, for any binary number, you write each of its bits as a power of 2 (in decimal) and then you add up these decimal numbers, e.g. 11010 2 = 16 + 8 + 2 = 26. The other direction is more challenging. How do you convert from a decimal number to a binary number? Here is an algorithm for doing so which is so simple you could have learned it in grade school. The algorithm repeatedly divides by 2 and the remainder bits give us the binary representation. Recall that / is the integer division operation which ignores the remainder i.e. fractions. If you want the remainder of the division, use % which is sometimes called the mod (or modulus) operator. Algorithm 1 Convert decimal to binary INPUT: a number m expressed in base 10 (decimal) OUTPUT: the number m expressed in base 2 using a bit array b[ ] i 0 while m > 0 do b[i] m%2 m m/2 i i + 1 end while Example: Convert 241 to binary i m b[ ] 0 241 1 120 1 2 60 0 3 30 0 4 15 0 5 7 1 6 3 1 7 1 1 8 0 1 9 0 0 10 : : 2

and so (241) 10 = (11110001) 2. Note that there are an infinite number of 0 s on the left which are higher powers of 2 which we ignore. What does this algorithm work? To answer this question, it helps to recall a few properties of multiplication and division. Let s go back to base 10 where we have a better intuition. Suppose we have a positive integer m which is written in decimal (as a sum of powers of 10) and we then multiply m by 10. There is a simple way to get the result, 10 m, namely shift the digits left by one place and put a 0 in the rightmost position. So, 238 10 = 2380 and the reason is 238 10 = (2 10 2 + 2 10 1 + 8 10 0 ) 10 = 2 10 3 + 3 10 2 + 8 10 1 + 0 10 0. Similarly, to divide a number m by 10, we shift the digits to the right 238/10 = (2 10 2 + 3 10 1 + 8 10 0 )/10 = 2 10 1 + 3 10 0 We have dropped the rightmost digit 8 (which becomes the remainder) since we are doing integer division, and thus ignoring terms with negative powers of 10 i.e 8 10 1. In binary, the same idea holds. If we represent a number m in binary and multiply by 2, we shift the bits to the left by one position and put a 0 in the rightmost position. So, e.g. if then multiplying by 2 gives m = (11010) 2 = 1 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 (110100) 2 = 1 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 0 2 1 + 0 2 0. If we divide by 2, then we shift right by one position and drop the rightmost bit (which becomes the remainder). Let s put the left shift and right shifts together. For any positive integer m, we can write for example, m = 10 (m/10) + (m%10), 549 = 540 + 9 = 10 (549/10) + (549 % 10). More generally, for any positive integer call it base we can write and in particular, in binary we use base = 2, so m = base (m/base) + (m % base), m = 2 (m/2) + (m % 2). Now let s apply these ideas to the algorithm. Representing a positive integer m in binary means that we write it m = n 1 i=0 b i 2 i 3

where b i is a bit, i.e either 0 or 1. So we write m in binary as a bit sequence (b n 1 b n 2... b 2 b 1 b 0 ). In particular, m % 2 = b 0 m / 2 = (b n 1... b 2 b 1 ) Thus, the algorithm essentially just uses repeated division and mod to read off the bits of the binary representation of the number. If you are still not convinced, let s run another example where we know the answer from the start and we ll see that the algorithm does the correct thing. Suppose our number is m = 241, which is (11110001) 2 in binary. i m b[i] 0 11110001 1 1111000 1 2 111100 0 3 11110 0 4 1111 0 5 111 1 6 11 1 7 1 1 8 0 1 and so the remainders are just the bits used in the binary representation of the number. Interestingly, the same algorithm works for any base. For example, let s convert 238 from decimal to base 5, that is, let s write 238 as a sum of powers of 5. We apply the same algorithm as earlier but now we divide by 5 at each step and take the remainder. 238 47 3 9 2 1 4 0 1 and so (238) 10 = (1423) 5. Verify this by converting the latter back into decimal by summing powers of 5. Binary fractions Up to now we have only talked about integers. We next talk about binary representations of fractional numbers, that is, numbers that lie between the integers. Take a decimal number such as 26.375. We write this as: The. is called the decimal point. (26.375) 10 = 2 10 1 + 6 10 0 + 3 10 1 + 7 10 2 + 5 10 2. 4

One uses an analogous representation using binary numbers, e.g. (11010.011) 2 = 1 2 4 + 1 2 3 + 0 2 2 + 1 2 1 + 0 2 0 + 0 2 1 + 1 2 2 + 1 2 3 where. is called the binary point. Check for yourself that this is the same number as above, namely 16 + 8 + 2 + 0.25 + 0.125 = 26.375. How do we convert from decimal to binary for such fractional numbers in general? Suppose we have a number x that is written in decimal and has a finite number of digits to the right of the binary point. Let s look at a particular example. This is different from the one given in the slides. Let x = 4.63 and let s convert it to binary. Since x = 4 +.63, we know the answer will have the form 100. since 100 is the binary representation of 4 and.63 is a sum of powers of negative powers of 2. So we just need to find the bits to the right of the binary point. To get the first say five bits, we multiply and divide by 2 five times (or alternatively, as was suggested in class, we just directly multiply 0.67 by 2 5 = 32 and also divide by 2 5 ). 0.67 = 1.34 2 1 = 2.68 2 2 = 5.36 2 3 = 10.72 2 4 = 21.44 2 5 We convert (21) 10 from decimal to binary which gives (10101. ) 2 and then we shift the binary point left by five places. Thus.67 =.10101, and so 4.63 = 100.10101. If we drop off the unspecified part to have a finite number of bits only, then we have an approximation. As was observed in class, we can sometimes obtain a better approximation than truncating (chopping) the unknown bits. In this example, the approximation error is 0.44 2 5 and since 0.44 is closer to 0 than to 1, we are better of truncating. For other examples, however, it might be better to round up. This is the case of the one example in the slides, where we had (.247) 10 = 3.952 2 4 = (.0011 ) 2 Since.952 is closer to 1 than to 0, we would obtain a better approximation we replaced.954 2 4 by 1 2 4, and so the approximation would be (.0011) 2 + 2 4 = (.0100) 2. 5