New version page

# Wright CEG 320 - Bits, Data Types & Operations

Pages: 26
Documents in this Course
Unformatted text preview:

Chapter 2Chapter 2Bits, Data Types & OperationsBits, Data Types & Operationsz Integer Representationz Floating‐point Representationz Other data typesWhy do Computers use Base 2?Why do Computers use Base 2?z Base 10 Number Representation– Natural representation for human transactionsHd Il El i ll3.5 = 3×100+ 5×10-1zHard to Implement Electronically–Hard to storez ENIAC (First electronic computer) used 10 vacuum tubes / digit–Hard to transmitNdhi h ii t d 10 illl il izNeed high precision to encode 10 signal levels on single wire– Messy to implement digital logic functionsz Addition, multiplication, etc.z Binary RepresentationSpecifically the devices that make up a computer are switches that can be–Specifically, the devices that make up a computer are switches that can be on or off, i.e. at high or low voltage.–Easy to store with bistable elements– Reliably transmitted on noisy and inaccurate wires zExamples101zExamples–Represent 3.510as 11.12–Represent 1521310as 111011011011012–Represent 1.2010as 1.0011001100110011…2zBinary floating point number cannot exactly represent \$1 203.5 = 1×21+ 1×20+ 1×2-1+…2Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyzBinary floating point number cannot exactly represent \$1.20Data representation in binaryData representation in binaryz We need a way to represent information (data) in a form that is mutually comprehensible by human and machine.Hex Binaryx0 0000x1 0001that is mutually comprehensible by human and machine.–We have to develop schemes for representing all conceivable types of information – integers, characters, floating point numbers, language, images, actions, etc.x2 0010x3 0011x40100z Binary digITs (Bits): sequences of 0’s and 1’s that help humans keep track of the current flows stored/processed in the computerx40100x5 0101x6 011070111–Using base‐2 provide us with two symbols to work with: we can call them on & off, or (more usefully) 0 and 1.–We group bits together to allow representation of more l if tix70111x8 1000x9 1001complex information–For ease of use, Computer scientists usually represent quarters of bits in HexadecimalegxA13F vs 1010000100111111xA 1010xB 1011xC 11003Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & Assembly–eg. xA13F vs. 1010000100111111xD 1101xE 1110xF 1111Unsigned Binary IntegersUnsigned Binary IntegersY = “abc” = a.22 + b.21+ c.203-bits 5-bits 8-bitsN = number of bits(where the digits a, b, c can each take on the values of 0 or 1 only)0 000 00000 000000001 001 00001 00000001Range is: 0 ≤ i < 2N–1Umin = 02 010 00010 000000103 011 00011 00000011Umin = 0Umax = 2N–1Problem:4 100 00100 00000100Problem:• How do we represent negative numbers?4Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblygSigned MagnitudeSigned Magnitudez Leading bit is the sign bit-4 10100-3 10011-2 10010Range is: -2N-1+ 1 < i < 2N-1–1Y = “abc” = (-1)a (b.21+ c.20)-1 10001-0 10000000000gSmin = -2N-1+ 1 Smax = 2N-11+000000+1 00001+200010Smax = 2N1–1Problems:+200010+3 00011+400100• How do we do addition/subtraction?• We have two numbers for zero (+/-)!5Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & Assembly+400100Two’s ComplementTwo’s Complementz Transformation-16 10000–To transform a into ‐a, invert all bits in a and add 1 to the result……-3 11101211110Ri2N1<i <2N11-211110-1 111110 00000Range is: -2N-1< i < 2N-1–1Tmin = -2N-1Tmax = 2N-1–1+1 00001+2 00010Advantages:• Operations need not check the sign+3 00011……+1501111• Only one representation for zero• Efficient use of all the bits6Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & Assembly+1501111Manipulating Binary numbers Manipulating Binary numbers ‐‐ 11z Binary to Decimal conversion & vice‐versa–A 4 bit binary number A = a3a2a1a0corresponds to:a3 x 23+ a2 x 22+ a1 x 21+ a0 x 20= a3 x 8 + a2 x 4 + a1 x 2 + a0 x 1(where ai= 0 or 1 only)–A decimal number can be broken down by iteratively determining the highest power of two that “fits” in the number:e.g. (13)10=>e.g. (63)10=>e.g. (0.63)10=>–In the 2’s complement representation, leading zeros do notaffect the value of a positive binary number, and leading ones do notaffect the value of a negative number. So:01101 = 00001101 = 13 and 11011 = 11111011 =501101 = 00001101 = 13 and 11011 = 11111011 = ‐500001101 x0D11111011xFB7Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & Assembly00001000 => 8 (as expected!) x08Manipulating Binary numbers Manipulating Binary numbers ‐‐ 1010z Overflow–If we add the two (2’s complement) 4 bit numbers representing 7 and 5 we get :0111 => +7010150101=> +51100 => ‐4 (in 4 bit 2’s comp.)z We get ‐4, not +12 as we would expect !!zWe haveoverflowedthe range of 4 bit 2’s comp (8 to +7) so the result is invalidzWe have overflowedthe range of 4 bit 2s comp. (‐8 to +7), so the result is invalid.z Note that if we add 16 to this result we get back 16 ‐ 4 = 12–this is like “stepping up” to 5 bit 2’s complement representationI l if h f ii b d i l–In general, if the sum of two positive numbers produces a negative result, or vice versa, an overflow has occurred, and the result is invalid in that representation.8Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & AssemblyCS Reality #1CS Reality #1z The first place where the underlying hardware abstraction can’t be treated as a magic black box results in the need for data typesz You’ve got to understand binary encodings.–Can x = 20,000,000,000?–Is x2≥ 0?z Int’s:– 40000 * 40000 ‐‐> 1,600,000,000– 50000 * 50000 ‐‐> ?? Fl t’Y!zFloat’s: Yes!–Is (x + y) + z = x + (y + z)?z Unsigned & Signed Int’s: Yes!zFloat’s:zFloat s:– (1e20 + ‐1e20) + 3.14 ‐‐> 3.14–1e20 + (‐1e20 + 3.14) ‐‐> 0? 3? 3.1?–Is 4/5 = 4/5.0?9Wright State University, College of EngineeringDr. Doom, Computer Science & EngineeringCEG 320/520Comp. Org. & Assemblyz Maybe? (int) 4/5 = 0 what is 4/5.0?C++ Integer PuzzlesC++ Integer Puzzles– Assume machine with 32 bit

View Full Document Unlocking...