1 / 32

Binary numbers

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).

garvey
Download Presentation

Binary numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 11012 in base 10? • What is 110110 in base 10? • What is 11013 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 11022 a valid base 2 number? • Is 12644 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 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

  12. 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

  13. 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.

  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. • 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.)

  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. • 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.

  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. 24 = 16 23 = 8 22 = 4 21 = 2 20 = 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. 24 = 16 23 = 8 22 = 4 21 = 2 20 = 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.07812510 to base 2. Reading forward, the result is 1011.0001012.

  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 6110 to base 2. lsb msb Reading backwards, the result is 1111012.

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

  22. 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.

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

  24. 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?

  25. 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? same 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 8 base 2 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 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.

  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 16 base 10 base 2 base 8 0 0 0000 00 1 1 0001 01 2 2 0010 02 3 3 0011 03 4 4 0100 04 5 5 0101 05 6 6 0110 06 7 7 0111 07 8 8 1000 10 9 9 1001 11 a 10 1010 12 b 11 1011 13 c 12 1100 14 d 13 1101 15 e 14 1110 16 f 15 1111 17

  29. Problems: • Convert 7258 to base 2 to base 16 to base 10.

  30. Problems: • Convert 7258 to base 2 to base 16 to base 10. • 111 010 1012 • 1 1101 01012 = 1d516 • 1x162 + 13x161 + 5x160 = 46910

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

  32. Problem: Convert 3115 to X10 to Y2. • 3x52+1x51x1x50 = 75+5+1 = 8110.

More Related