Bits, Data Types, and Operations What Does the Computer Understand?

Bits, Data Types, and Operations What Does the Computer Understand?

Computer Programming Programming Language • Is telling the computer how to do something • Wikipedia Definition: Applies specific programming languages to solve specific computational problems with solutions Bits, Data Types, and Operations High-level Programming Language • A programming language with strong abstraction from the details of the computer Isolates the execution semantics of a computer architecture comparedtd to assem bl/lbly/low-lllevel • Advantage: Makes the process of developing a program simpler Based on slides © McGraw-Hill and more understandable Additional material © 2004/2005 Lewis/Martin Edited by Diana Palsetia What does the Computer Understand? What does the Computer process & store? At the lowest level, a computer has electronic “plumbing” An electronic device can represent uniquely only one Operates by controlling the flow of electrons through very fast of two things tiny electronic devices called transistors Each “0” and Each “1” is referred to as a Binary Digit or Bit Fundament unit of information storage The devices react to presence or absence of voltage To represent more things we need more bits Could react actual voltages but designing electronics then becomes complex E.g. 2 bits can represent four unique things: 00, 01, 10, 11 k bits can distinguish 2k distinct items Symbolically we represent Combination binary bits together can represent some 1. Presence of voltage as “1” information or data. E.g. 01000001 can be 2. Absence of voltage as “0” 1. Decimal value 65 2. Alphabet (or character) ‘A’ in ASCII notation 3. Command to be performed e.g. Performing Add operation CIT 593 3 CIT 593 4 1 Computer is a binary digital system Data Binary (base two) system: What kinds of data do we need to represent? Has two states: 0 and 1 Numbers – signed, unsigned, integers, real, floating point, complex, rational, irrational, … Text – characters, strings, … Digital system: Logical –true, false Finite number of symbols or bits Images – pixels, colors, shapes, … If we want represent 3 or more values then we require multiple bits Data type: “A particular representation is data type if there are operations in the computer that can operate on the information that is encoded All computers are characterized by number of bits the can in the representation.” (by Patt & Patel) store and process Most programming languages provide basic data types Example: 4, 8, 16, 32… Currently at 64-bit We’ll start with numbers… CIT 593 5 CIT 593 6 Unsigned Integers Unsigned Integers (cont.) Non-positional notation An n-bit unsigned integer represents 2n values Could represent a number (“5”) with a string of ones (“11111”) From 0 to 2n -1 Problems? 22 21 20 val Weighted positional notation 0 0 0 0 Like decimal numbers: “329” 0 0 1 1 “3” is worth 300, because of its position, while “9” is only worth 9 0 1 0 2 0 1 1 3 base-10 most least 1 0 0 4 significant significant (decimal) 329 101 base-2 1 0 1 5 (binary) 102 101 100 22 21 20 1 1 0 6 1 1 1 7 3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5 CIT 593 7 CIT 593 8 2 Base-2 Addition on Unsigned Data Signed Integers Positive integers Just like unsigned with zero in most significant bit 00101 = 5 Negative integers Sign-magnitude: set high-order bit to show negative, other bits are the same as unsigned 10101 = -5 One’s complement: flip every bit to represent negative 11010 =- 5 In either case, MS bit indicates sign: 0=positive, 1=negative Both sign-magnitude and 1’s complement have problem CIT 593 9 CIT 593 10 Problem Two’s Complement Sign-magnitude problem Idea Two representations of zero (+0 and –0) Find representation to make arithmetic simple and consistent 1’s complement problem Specifics Addition is complex For each positive number (X), assign value to its negative (-X), ¾Add two sign-magnitude numbers? such that X + (-X) = 0 with “normal” addition, ignoring carry out – e.g., try -12 + (13) 10011 (-12) +01101 (13) 00101 (5) 01001 (9) 1 00000 (0) +11011 (-5) + 10111(-9) 00000 (0) 00000 (0) ¾Need to add add back the carry into least significant bit (LSB) ¾Which will result in the correct result, -12+13 = 1 CIT 593 11 CIT 593 12 3 Two’s Complement (cont.) Two’s Complement Signed Integers If number is positive or zero Range of an n-bit number: -2n-1 through 2n-1 –1 Normal binary representation, zeroes in upper bit(s) Note: most negative number (-2n-1) has no positive counterpart If number is negative 3 2 1 0 3 2 1 0 Start with positive number 2 2 2 2 2 2 2 2 Flip every bit (i.e., take the one’s complement) 0 0 0 0 0 1 0 0 0 -8 Then add one 0 0 0 1 1 1 0 0 1 -7 0 0 1 0 2 1 0 1 0 -6 00101 (5) 01001 (9) 0 0 1 1 3 1 0 1 1 -5 (1’s comp) (1’s comp) 11010 10110 0 1 0 0 4 1 1 0 0 -4 + 1 + 1 0 1 0 1 5 1 1 0 1 -3 (-9) 11011 (-5) 10111 0 1 1 0 6 1 1 1 0 -2 0 1 1 1 7 1 1 1 1 -1 CIT 593 13 CIT 593 14 Converting Binary (2’s C) to Decimal More Examples 1. If leading bit is one, take two’s complement to get a positive number X = 00100111two 2. Add powers of 2 that have “1” in the n 2n =25+22+21+20 = 32+4+2+1 n 2n corresponding bit positions 0 1 =39 0 1 1 2 ten 1 2 3. If original number was negative, 2 4 2 4 add a minus sign 3 8 X = 11100110 3 8 4 16 two 4 16 5 32 -X=00011010 5 32 6 64 4 3 1 6 64 X = 01101000two =2 +2 +2 = 16+8+2 6 5 3 7 128 7 128 =2 +2 +2 = 64+32+8 8 256 =26ten 8 256 =104ten 9 512 X=-26 9 512 10 1024 ten 10 1024 Assuming 8-bit 2’s complement numbers. Assuming 8-bit 2’s complement numbers. CIT 593 15 CIT 593 16 4 Converting Decimal to Binary (2’s C) Converting Decimal to Binary (2’s C) n 2n First Method: Division Second Method: Subtract Powers of Two 0 1 1. Change to positive decimal number 1. Change to positive decimal number 1 2 2. Divide by two – remainder is least significant bit 2. Subtract largest power of two 2 4 3. Keep dividing by two until answer is zero , 3 8 recording remainders from right to left less than or equal to number 4 16 4. Append a zero as the MS bit; 3. Put a one in the corresponding bit position 5 32 if original number negative, take two’s complement 6 64 4. Keep subtracting until result is zero 7 128 5. Append a zero as MS bit; 8 256 X = 104 104/2 = 52 r0 bit 0 ten if original was negative, take two’s complement 9 512 52/2 =26 r0bit 1 10 1024 26/2 =13 r0bit 2 13/2 = 6 r1 bit 3 X = 104ten 104 - 64 = 40 bit 6 6/2 = 3 r0 bit 4 40 - 32 = 8 bit 5 3/2 = 1 r1 bit 5 8 - 8 = 0 bit 3 1/2 = 0 r1 bit 6 X=01101000two X=01101000two CIT 593 17 CIT 593 18 Operations: Arithmetic and Logical Addition Recall 2’s comp. addition is just binary addition A data type includes representation and operations Assume all integers have the same number of bits Operations for signed integers Ignore carry out Addition For now, assume that sum fits in n-bit 2’s comp. representation Subtraction ¾ Assuming 8-bit 2’s complement numbers Sign Extension Logical operations are also useful (104) (-10) AND 01101000 11110110 OR +11110000(-16) + 11110111 (-9) NOT 1 01011000(88)1 1 1 1 0 1 1 0 1 (-19) And. carry(discard) Overflow conditions for addition CIT 593 19 CIT 593 20 5 Subtraction Sign Extension Negate 2nd operand and add To get correct results Assume all integers have the same number of bits Must represent numbers with same number of bits Ignore carry out For now, assume tha t difference fit s i n n-bit 2’s comp. What if we just pad with zeroes on the left? representation 4-bit 8-bit 01101000(104) 11110110 (-10) 0100 (4) 00000100 (still 4) -00010000(16) - 11110111(-9) 1100 (-4) 00001100 (12, not -4) 01101000(104) 11110110(-10) Now, let’s replicate the MSB (the sign bit) (-16) (9) +11110000 + 00001001 4-bit 8-bit 01011000 (88)1111111 1 (-1) 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4) Assuming 8-bit 2’s complement numbers. CIT 593 21 CIT 593 22 Logical Operations Examples of Logical Operations Operations on logical TRUE or FALSE Two states: TRUE=1, FALSE=0 AND 11000101 Useful for clearing bits AND 00001111 A B A AND B A B A OR B A NOT A ¾AND witteo0h zero = 0 00000101 ¾AND with one = no change 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 OR 11000101 1 0 0 1 0 1 Useful for setting bits OR 1 1 1 1 1 1 00001111 ¾OR with zero = no change 11001111 ¾OR with one = 1 View n-bit number as a collection of n logical values NOT NOT 11000101 Operation applied to each bit independently (bitwise operators) Unary operation -- one argument 00111010 ¾Lot of use in water marking, cryptography, and new Flips every bit application that requires bit manipulation CIT 593 23 CIT 593 24 6 Overflow Number System Overflow is said to occur if result is too large to fit in the People like to use decimal numbers number of bits used in the representation.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us