Unit 3 Binary Representation
Total Page:16
File Type:pdf, Size:1020Kb
3.1 3.2 Unit 3 Binary Representation ANALOG VS. DIGITAL 3.3 3.4 Analog vs. Digital Analog vs. Digital • The analog world is based on continuous • Q. Which is better? events. Observations can take on (real) any • A. Depends on what you are trying to do. value. • Some tasks are better handled with analog • The digital world is based on discrete events. data, others with digital data. Observations can only take on a finite number Analog means continuous/real valued signals with of discrete values – an infinite number of possible values – Digital signals are discrete [i.e. 1 of n values] 3.5 3.6 Analog vs. Digital Analog vs. Digital • How much money is in my checking account? • How much do you love me? – Analog: I love you with all my heart!!!! – Analog: Oh, some, but not too much. – Digital: 3.2 x 10 3 MegaHearts – Digital: $243.67 3.7 3.8 The Real (Analog) World Digital is About Numbers • The real world is inherently analog. • In a digital world, numbers are used to represent all the possible discrete events • To interface with it, our digital systems need Numerical values to: – – Computer instructions (ADD, SUB, BLE, …) – Convert analog signals to digital values (numbers) – Characters ('a', 'b', 'c', …) at the input. – Conditions (on, off, ready, paper jam, …) – Convert digital values to analog signals at the • Numbers allow for easy manipulation output. – Add, multiply, compare, store, … • Analog signals can come in many forms • Results are repeatable – Voltage, current, light, color, magnetic fields, – Each time we add the same two number we get pressure, temperature, acceleration, orientation the same result 3.9 3.10 Interpreting Binary Strings • Given a string of 1’s and 0’s, you need to know the representation system being used, before you can understand the value of those 1’s and 0’s. • ______________________________________ 01000001 = ? Unsigned Binary system ASCII DIGITAL REPRESENTATION BCD System system 65 10 ‘A’ ASCII 41 BCD 3.11 3.12 Binary Representation Systems • Integer Systems • Codes – Unsigned – Text • Unsigned (Normal) binary • ASCII / Unicode – Signed – Decimal Codes • Signed Magnitude • BCD (Binary Coded Decimal) • 2’s complement / (8421 Code) • Excess-N* • 1’s complement* • Floating Point OVERVIEW – For very large and small (fractional) numbers * = Not fully covered in this class 3.13 3.14 4 Skills • We will teach you 4 skills that you should know and be able to apply with confidence – Convert a number in any base (base r) to decimal (base 10) – Convert a decimal number (base 10) to binary – Use the shortcut for conversion between binary Using positional weights/place values (base 2) and hexadecimal (base 16) BASE R TO BASE 10 – Understand the finite number of combinations that can be made with n bits (binary digits) and its implication for codes including ASCII and Unicode 3.15 3.16 Number Systems Anatomy of a Decimal Number • Number systems consist of • A number consists of a string of explicit coefficients (digits). 1. ________________ • Each coefficient has an implicit place value which is a _______ of the base. 2. ___ coefficients [__________] • The value of a decimal number (a string of decimal • Human System: Decimal (Base 10): coefficients) is the sum of each coefficient times it place value radix 0,1,2,3,4,5,6,7,8,9 (base) • Computer System: Binary (Base 2): 0,1 (934) 10 = 9* ___ + 3* ___ + 4* ____ = _____ • Human systems for working with computer systems (shorthand for human to read/write binary) Implicit place values Explicit coefficients – _____________________________________ – _____________________________________ (3.52) 10 = 3* ____ + 5* ____ + 2* ____ = ____ 3.17 3.18 Anatomy of a Binary Number General Conversion From Base r to Decimal • Same as decimal but now the coefficients • A number in base r has place values/weights are 1 and 0 and the place values are the that are the powers of the base powers of 2 • Denote the coefficients as: ai Most Significant Least Significant 3 2 1 0 -1 -2 Digit (MSB) Bit (LSB) (a3a2a1a0.a -1a-2)r = a 3*r + a 2*r + a 1*r + a 0*r + a -1*r + a -2*r Left-most digit = Right-most digit = (1011) 2 = 1*_ _ + 0* _ + 1* __ + 1* __ Most Significant Least Significant Digit (MSD) Digit (LSD) radix (base) place values coefficients = powers of 2 Nr => _______=> D 10 Number in base r Decimal Equivalent 3.19 3.20 Examples Binary Examples (746) 8 = (1001.1) 2 = (1A5) 16 = (10110001) 2 = (AD2) 16 = 3.21 3.22 Powers of 2 Unique Combinations 20 = 1 Given n digits of base r, how many unique numbers 21 = 2 • 22 = 4 can be formed? __ 3 2 = 8 – What is the range? [________] 24 = 16 25 = 32 2-digit, decimal numbers (r=10, n=2) 26 = 64 0-9 0-9 27 = 128 1024 512 256 128 64 32 16 8 4 2 1 3-digit, decimal numbers (r=10, n=3) 28 = 256 29 = 512 4-bit, binary numbers (r=2, n=4) 210 = 1024 0-1 0-1 0-1 0-1 6-bit, binary numbers (r=2, n=6) Main Point: Given n digits of base r, ___ unique numbers can be made with the range [________] 3.23 3.24 Approximating Large Powers of 2 • Often need to find decimal approximation of a large powers of 2 216 = 2 6 * 2 10 like 2 16 , 2 32 , etc. ≈ • Use following approximations: – 210 ≈ _________________ 224 = – 220 ≈ _________________ – 230 ≈ _________________ 28 – 240 ≈ _________________ 2 = "Making change" • For other powers of 2, decompose into product of 2 10 or 2 20 or 2 30 and a BASE 10 TO BASE 2 OR BASE 16 power of 2 that is less than 2 10 232 = – 16-bit half word: 64K numbers – 32-bit word: 4G numbers – 64-bit dword: 16 million trillion numbers 3.25 3.26 Decimal to Unsigned Binary Decimal to Unsigned Binary • To convert a decimal number, x, to binary: 73 10 = – Only coefficients of 1 or 0. So simply find place values 128 64 32 16 8 4 2 1 that add up to the desired values, starting with larger 87 = place values and proceeding to smaller values and place 10 a 1 in those place values and 0 in all others 145 10 = 25 = 10 0.625 = 32 16 8 4 2 1 10 .5 .25 .125 .0625 .03125 3.27 3.28 Decimal to Another Base • To convert a decimal number, x, to base r: – Use the place values of base r (powers of r). Starting with largest place values, fill in coefficients that sum up to desired decimal value without going over. Shortcuts for Converting Binary (r=2), Hexadecimal (r=16) and Octal (r=8) SHORTHAND FOR BINARY 75 10 = hex 256 16 1 3.29 3.30 Binary, Octal, and Hexadecimal Binary to Octal or Hex • Octal (base 8 = 2 3) • Hex (base 16=2 4) • Make groups of 3 bits • Make groups of 4 bits starting from radix point starting from radix point • 1 Octal digit ( _ ) can • 1 Hex digit ( _ ) can 8 16 and working outward and working outward represent: ________ represent: 0-F (_____) • Add 0’s where • Add 0’s where necessary • 3 bits of binary (_ _ _) • 4 bits of binary necessary 2 • Convert each group of 3 can represent: (_ _ _ _) can represent: • Convert each group of 4 2 to an octal digit to an octal digit 000-111 = ________ 0000-1111= ______ • Conclusion… • Conclusion… 101001110.11 101001110.11 __Octal digit = __ bits __ Hex digit = ___ bits 3.31 3.32 Octal or Hex to Binary Hexadecimal Representation • Expand each octal digit • Expand each hex digit • Since values in modern computers are many bits, we to a group of 3 bits to a group of 4 bits use hexadecimal as a shorthand notation (4 bits = 1 hex digit) – 11010010 = D2 hex or 0xD2 if you write it in C/C++ 317.2 8 D93.8 16 – 0111011011001011 = 76CB hex or 0x76CB if you write it in C/C++ 3.33 3.34 Binary Representation Systems • Integer Systems • Codes – Unsigned – Text • Unsigned (Normal) binary • ASCII / Unicode – Signed – Decimal Codes • Signed Magnitude • BCD (Binary Coded Decimal) • 2’s complement / (8421 Code) • 1’s complement* ASCII & Unicode • Excess-N* BINARY CODES • Floating Point – For very large and small (fractional) numbers * = Not covered in this class 3.35 3.36 Binary Codes BCD (If Time Permits) • Rather than convert a decimal number to binary which may lose • Using binary we can represent any kind of some precision (i.e. 0.1 10 = infinite binary fraction), BCD represents each decimal digit as a separate group of bits (exact information by coming up with a code decimal precision) n – Each digits is represented as a ___________ number (using place values 8,4,2,1 • Using n bits we can represent 2 distinct items for each dec. digit) – Often used in financial and other applications where decimal precision is needed Colors of the rainbow: Letters: (439) This is the Binary •Red = 000 •‘A’ = 00000 10 Coded Decimal (BCD) •Orange = 001 •‘B’ = 00001 representation of 439 •Yellow = 010 •‘C’ = 00010 •Green = 100 . BCD Representation: •Blue = 101 . This is the binary •Purple = 111 . representation of 439 •‘Z’ = 11001 Unsigned Binary Rep.: (i.e. using power of 2 110110111 2 place values) Important: Some processors have specific instructions to operate on #’s represented in BCD 3.37 3.38 ASCII Code ASCII Table • Used for representing text characters LSD/MSD 0 1 2 3 4 5 6 7 0 NULL DLW SPACE 0 @ P ` p • Originally 7-bits but usually stored as 8-bits = 1- 1 SOHDC1 ! 1 A Q a q byte in a computer 2 STXDC2 “ 2 B R b r 3 ETXDC3 # 3 C S c s • Example: 4 EOTDC4 $ 4 D T d t "Hello\n"; 5 ENQNAK % 5 E U e u – 6 ACK SYN & 6 F V f v – Each character is converted to ASCII equivalent 7 BELETB ‘ 7 G W g w • ‘H’ = 0x48, ‘e’ = 0x65, … 8 BSCAN ( 8 H X h x 9 TABEM ) 9 I Y i y • \n = newline character is represented by either one or two ASCII A LFSUB * : J Z j z character B VTESC + ; K [ k { C FFFS , < L \ l | D CRGS - = M ] m } E SORS .