Upload
garvey
View
24
Download
3
Embed Size (px)
DESCRIPTION
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 computers are made primarily of transistors which act as switches). - PowerPoint PPT Presentation
Citation preview
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 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)
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.
Binary numbers
• Can we represent numbers is other bases (radix)?– What is 11012 in base 10?
– What is 110110 in base 10?
– What is 11013 in base 10?
– Note that they are different!
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!
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!
Binary numbers
• What are the range of digits for base 10 numbers?– For binary?– For base 5?– For some arbitrary base k?
– Is 11022 a valid base 2 number?
– Is 12644 a valid base 4 number?
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?
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)
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).
Common ranges of values (Java)
• int -2 billion to +2 billion
• double 4.9x10-324 (closest to 0)to 1.8x10+308
• float 1.4x10-45 (closest to 0) to 3.4x10+38
Converting from base 10 to base 2
• Convert 6110 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.
• 28 = 256• 27 = 128• 26 = 64• 25 = 32• 24 = 16• 23 = 8• 22 = 4• 21 = 2• 20 = 1
Converting from base 10 to base 2• Convert 6110 to base 2.
• Method 1: Subtract largest power of 2 repeatedly.1. Make a table of powers of 2.
• 28 = 256• 27 = 128• 26 = 64• 25 = 32• 24 = 16• 23 = 8• 22 = 4• 21 = 2• 20 = 1
2. Repeatedly subtract(largest power of 2)
msb
lsb
Reading forward, the result is 1111012. 0
211
201
214
5
218
13
2116
29
2132
61
0
1
2
3
4
5
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.
• 34 = 81• 33 = 27• 32 = 9• 31 = 3• 30 = 1
Step 2: Repeatedly subtract (largest) multiples of largest power of 3. (Why? Recall digit range for a particular base.)
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.• 34 = 81• 33 = 27• 32 = 9• 31 = 3• 30 = 1
Step 2: Repeatedly subtract (largest) multiples of largest power of 3.
• Convert 6110 to base 3.
msd
lsd
Reading forward, the result is 20213.
0
311
1
326
307
3254
61
0
1
2
3
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!
Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction)
Step 1: Create table of powers of 2.24 = 1623 = 822 = 421 = 220 = 12-1 = 0.52-2 = 0.252-3 = 0.1252-4 = 0.06252-5 = 0.031252-6 = 0.015625
Step 2: Subtract largest power of 2 repeatedly.
Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction)
Step 1: Create table of powers of 2.24 = 1623 = 822 = 421 = 220 = 12-1 = 0.52-2 = 0.252-3 = 0.1252-4 = 0.06252-5 = 0.031252-6 = 0.015625
Step 2: Subtract largest power of 2 repeatedly.
Convert 11.07812510 to base 2.
Reading forward, the result is 1011.0001012.
0
21015625.0
20015625.0
210625.0
20
20
20078125.0
211
078125.1
212
20078125.3
218
078125.11
6
5
4
3
2
1
0
1
2
3
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
Method 2: Repeated (integer) division (with remainder) by desired base.
Convert 6110 to base 2. lsb
msb
Reading backwards, the result is 1111012.
12%102/1
12%312/3
12%732/7
12%1572/15
02%30152/30
12%61302/61
Method 2: Repeated (integer) division (with remainder) by desired base.
Convert 6110 to base 3.
lsd
msd
Reading backwards, the result is 20213.
23%203/2
03%623/6
23%2063/20
13%61203/61
Method 2 for fp numbers
• Convert 61.687510 to base 2.– We just saw how to convert 6110 to base 2 (by
repeated division with desired base).– So all we need to do is convert 0.687510 to base 2.
– This can (analogously) be accomplished by repeated multiplication by the desired base.
Method 2
• Convert 0.687510 to base 2 by repeated multiplication by desired base.
• Then read integer part forwards.
210 1011.06875.0
0.020.0
0.020.0
0.125.0
5.1275.0
75.02375.0
375.126875.0
Method 2
• Convert 0.110 to base 2 by repeated multiplication by desired base.
• Then read integer part forwards.
Do you see the repetition/loop?
210 ...000110011.01.0
4.022.0
2.126.0
6.128.0
8.024.0
4.022.0
2.021.0
Method 2
• Convert 0.110 to base 2 by repeated multiplication by desired base.
• Then read integer part forwards.
Do you see the repetition/loop?
210 ...000110011.01.0
4.022.0
2.126.0
6.128.0
8.024.0
4.022.0
2.021.0
same
same
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).
Other important bases related to base 2: base 8
• Octal, base 8– Digits: 0..7
base 8 base 20 0001 0012 0103 0114 1005 1016 1107 111
• Problems:– Convert 1008 to base 10.
– Convert 1008 to base 2.
– Convert 11012 to base 10.
– Convert 11012 to base 8.
– Convert 7028 to base 10.
– Convert 7028 to base 2.
– Convert 8028 to base 2.
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 16 base 10 base 2 base 80 0 0000 001 1 0001 012 2 0010 023 3 0011 034 4 0100 045 5 0101 056 6 0110 067 7 0111 078 8 1000 109 9 1001 11a 10 1010 12b 11 1011 13c 12 1100 14d 13 1101 15e 14 1110 16f 15 1111 17
Problems:
• Convert 7258 to base 2 to base 16 to base 10.
Problems:
• Convert 7258 to base 2 to base 16 to base 10.– 111 010 1012
– 1 1101 01012 = 1d516
– 1x162 + 13x161 + 5x160 = 46910
Convert 153.51310 to base 8.Approach: convert 153 and 0.513
separately.
208/2
328/19
1198/153
984.18248.0
248.58656.0
656.68832.0
832.08104.0
104.48513.0
So the result is 231.40651… base 8.
remainder
Problem: Convert 3115 to X10 to Y2.
• 3x52+1x51x1x50 = 75+5+1 = 8110.
8111664
1010001
msb 102/1
012/2
122/5
052/10
0102/20
0202/40
lsb 1402/81
2