Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary numbers. Primary memory Memory = where programs and data are stored – Unit = bit “BIT” is a contraction for what two words? Either a 1 or a 0 (because.

Similar presentations


Presentation on theme: "Binary numbers. Primary memory Memory = where programs and data are stored – Unit = bit “BIT” is a contraction for what two words? Either a 1 or a 0 (because."— Presentation transcript:

1 Binary numbers

2 Primary memory Memory = where programs and data are stored – Unit = bit “BIT” is a contraction for what two words? Either a 1 or a 0 (because computers are made primarily of transistors which act as switches). – Smallest addressable (i.e., can be read from or written to memory) unit is the byte. byte = 8 bits (an 8-bit number)

3 Binary numbers How do computers represent numbers and do arithmetic? How do we? What digits do we use? Why? – What is 903? 9 is the MSD above; 3 is the LSD.

4 Binary numbers Can we represent numbers is other bases (radix)? – What is 1101 2 in base 10? – What is 1101 10 in base 10? – What is 1101 3 in base 10? – Note that they are different!

5 Potential programming asssignment Write a program that reads in a string (not an int) representing a binary number and outputs the corresponding base 10 number. Do not use any methods from Java’s Integer class!

6 Potential programming asssignment Write a program that reads in a string (not an int) (representing a # in base b), reads in the base b (an int), and outputs the corresponding base 10 number. (What Java String method can be used to obtain an individual char at a particular position in a string?) Do not use any methods from Java’s Integer class!

7 Binary numbers What are the range of digits for base 10 numbers? – For binary? – For base 5? – For some arbitrary base k? – Is 1102 2 a valid base 2 number? – Is 1264 4 a valid base 4 number?

8 Numbers in mathematics I = the integers – Finite or infinite? R = the reals – Contains rational numbers like 2/7 and irrational numbers like  – Finite or infinite? C = the complex numbers – Finite or infinite?

9 Numbers in computers All represented by a finite sequence of bits (1’s and 0’s). I = the integers – Finite/bounded. – char (8 bits), short (16 bits), int (32 bits), long long (64 bits) R = the reals – Finite/bounded. (So what about  ?) – float (32 bits), double (64 bits), long double (128 bits)

10 Numbers in computers R = the reals – Finite/bounded. (So what about  ?) – Representation is called floating point. – float (32 bits), double (64 bits), long double (128 bits) – Can also be represented by fixed point and BCD (binary coded decimal).

11 Common ranges of values (Java) int-2 billionto+2 billion double4.9x10-324 (closest to 0) to1.8x10+308 float1.4x10-45 (closest to 0) to3.4x10+38

12 Converting from base 10 to base 2 Convert 61 10 to base 2. Method 1: Subtract largest power of 2 repeatedly. – Also works for both ints and floats. – First, make a table of powers of 2. 2 8 = 256 2 7 = 128 2 6 = 64 2 5 = 32 2 4 = 16 2 3 = 8 2 2 = 4 2 1 = 2 2 0 = 1

13 Converting from base 10 to base 2 Convert 61 10 to base 2. Method 1: Subtract largest power of 2 repeatedly. 1. Make a table of powers of 2. 2 8 = 256 2 7 = 128 2 6 = 64 2 5 = 32 2 4 = 16 2 3 = 8 2 2 = 4 2 1 = 2 2 0 = 1 2. Repeatedly subtract (largest power of 2) msb lsb Reading forward, the result is 111101 2.

14 Use this method to convert from base 10 to base 3. Method 1: Subtract largest powers of 3 repeatedly. Step 1: Create table of powers of 3. 3 4 = 81 3 3 = 27 3 2 = 9 3 1 = 3 3 0 = 1 Step 2: Repeatedly subtract (largest) multiples of largest power of 3. (Why? Recall digit range for a particular base.)

15 Use this method to convert from base 10 to base 3. Method 1: Subtract largest powers of 3 repeatedly. Step 1: Create table of powers of 3. 3 4 = 81 3 3 = 27 3 2 = 9 3 1 = 3 3 0 = 1 Step 2: Repeatedly subtract (largest) multiples of largest power of 3. Convert 61 10 to base 3. msd lsd Reading forward, the result is 2021 3.

16 Potential programming assignment Write a program that takes as input a base 10 number (an int) and outputs the corresponding base 2 number. Do I need to remind you again? Do not use any methods from Java’s Integer class!

17 Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction) Step 1: Create table of powers of 2. 2 4 = 16 2 3 = 8 2 2 = 4 2 1 = 2 2 0 = 1 2 -1 = 0.5 2 -2 = 0.25 2 -3 = 0.125 2 -4 = 0.0625 2 -5 = 0.03125 2 -6 = 0.015625 Step 2: Subtract largest power of 2 repeatedly.

18 Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction) Step 1: Create table of powers of 2. 2 4 = 16 2 3 = 8 2 2 = 4 2 1 = 2 2 0 = 1 2 -1 = 0.5 2 -2 = 0.25 2 -3 = 0.125 2 -4 = 0.0625 2 -5 = 0.03125 2 -6 = 0.015625 Step 2: Subtract largest power of 2 repeatedly. Convert 11.078125 10 to base 2. Reading forward, the result is 1011.000101 2.

19 Conversion methods Method 1. Using a table, repeatedly subtract largest multiple. - directly works for both ints and floats - requires a table Method 2: Repeated (integer) division (with remainder) by desired base. - works for ints but needs to be “modified” to work for floats - doesn’t require a table

20 Method 2: Repeated (integer) division (with remainder) by desired base. Convert 61 10 to base 2.lsb msb Reading backwards, the result is 111101 2.

21 Method 2: Repeated (integer) division (with remainder) by desired base. Convert 61 10 to base 3. lsd msd Reading backwards, the result is 2021 3.

22 Method 2 for fp numbers Convert 61.6875 10 to base 2. – We just saw how to convert 61 10 to base 2 (by repeated division with desired base). – So all we need to do is convert 0.6875 10 to base 2. – This can (analogously) be accomplished by repeated multiplication by the desired base.

23 Method 2 Convert 0.6875 10 to base 2 by repeated multiplication by desired base. Then read integer part forwards.

24 Method 2 Convert 0.1 10 to base 2 by repeated multiplication by desired base. Then read integer part forwards. Do you see the repetition/loop?

25 Method 2 Convert 0.1 10 to base 2 by repeated multiplication by desired base. Then read integer part forwards. Do you see the repetition/loop? same

26 Converting between arbitrary bases In general, to convert from base u to base v, we typically: – Convert from base u to base 10. – Then convert from base 10 to base v. When converting from base 2 to two other important bases, we can skip the intermediate conversion through base 10 and convert directly from base 2 to these two other bases (and vice versa).

27 Other important bases related to base 2: base 8 Octal, base 8 – Digits: 0..7 base 8base 2 0000 1001 2010 3011 4100 5101 6110 7111 Problems: – Convert 100 8 to base 10. – Convert 100 8 to base 2. – Convert 1101 2 to base 10. – Convert 1101 2 to base 8. – Convert 702 8 to base 10. – Convert 702 8 to base 2. – Convert 802 8 to base 2.

28 Other important bases related to base 2: base 16 Hex (hexadecimal), base 16 – Digits: 16 of them (more than base 10) so we’ll will need 6 extra symbols: {0..9,a..f} base 16base 10base 2base 8 00000000 11000101 22001002 33001103 44010004 55010105 66011006 77011107 88100010 99100111 a10101012 b11101113 c12110014 d13110115 e14111016 f15111117

29 Problems: Convert 725 8 to base 2 to base 16 to base 10.

30 Problems: Convert 725 8 to base 2 to base 16 to base 10. – 111 010 101 2 – 1 1101 0101 2 = 1d5 16 – 1x16 2 + 13x16 1 + 5x16 0 = 469 10

31 Convert 153.513 10 to base 8. Approach: convert 153 and 0.513 separately. So the result is 231.40651… base 8. remainder

32 Problem: Convert 311 5 to X 10 to Y 2. 3x5 2 +1x5 1 x1x5 0 = 75+5+1 = 81 10.


Download ppt "Binary numbers. Primary memory Memory = where programs and data are stored – Unit = bit “BIT” is a contraction for what two words? Either a 1 or a 0 (because."

Similar presentations


Ads by Google