Presentation is loading. Please wait.

Presentation is loading. Please wait.

S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.

Similar presentations


Presentation on theme: "S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems."— Presentation transcript:

1 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems Integer data types Unsigned integers Signed integers Signed-magnitude 1’s complement 2’s complement 2’s complement integers Number base conversions Arithmetic operations Status bits – Negative, Zero, Carry, & Overflow Floating point data type ASCII code

2 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Number Systems Used in a Computer The most commonly used number systems in digital computers are: Decimal Binary Octal Hexadecimal Number System Radix (base) Digits used Decimal 10 0, 1,.....,9 Binary 2 0, 1 Octal 8 0, 1,......, 7 Hexadecimal 16 0, 1,....., 9, A, B,....., F

3 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Unsigned Integers No distinction is made between positive and negative numbers. An unsigned integer S can be represented as: S = s n-1 s n-2..... s 1 s 0 r where s i = digits allowed in the corresponding number system for S n = number of integer digits r = radix (base) of the number system Examples:S = 562 10 S = 101110 2 S = 4257 8 S = 2CD59 16

4 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Signed Integers When it becomes necessary to specify the sign of a number (positive or negative) the following two representations can be employed. Signed-magnitude representation Complement representation 1’s complement 2’s complement

5 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Signed Integers: Signed-magnitude Representation An additional binary bit (sign bit) is added to the most significant bit position of the binary string that represents the magnitude. The sign bit will be 0 for a positive binary number and 1 for a negative binary number. Example:S = 1101101 2 S = 0111010 2

6 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Complement Representation 1’s complement of a binary number Simply complement the individual bits of the binary number. Example:Let N 2 = 101101 (unsigned) 1’s complement of N 2 = ? 2’s complement of a binary number Add a 1 to the 1’s complement. Example:Let N 2 = 101101 (Unsigned) 2’s complement of N 2 = ?

7 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu 2’s complement Integers The 2’s complement integer representation of a positive number is: 2’s complement integer = s N where“s”, the sign bit = 0 N is the magnitude, in binary, of the given number The 2’s complement integer representation of a negative number is: 2’s complement integer = s [2’s complement of N] where “s”, the sign bit = 1 N is the magnitude, in binary, of the given number

8 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu 2’s complement Integers (Continued) Example 1: Compute the 2’s complement integer representation of the decimal number +232 Example 2: Compute the 2’s complement integer representation of the decimal number -232

9 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Number Base Conversions Binary to Decimal Conversion 1. If leading bit (most significant bit, MSB) is 1, take two’s complement of the magnitude bits to get a positive number. 2. Add powers of 2 that have “1” s in the corresponding bit positions. 3. If original number was negative, add a minus sign.

10 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Binary to Decimal Conversion - Examples Example 1: Let N 2 = 01101000. Compute N 10. Example 2: Let N 2 = 00100111. Compute N 10. Example 3: Let N 2 = 11100110. Compute N 10.

11 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Number Base Conversions (Continued) Decimal to Binary Conversion Method used: Repeated Division 1. Divide the magnitude of the decimal number by 2. The remainder is the least significant bit (LSB) of the binary. 2. Keep dividing by 2 until the answer is zero, writing the remainders from right to left. 3. Append a zero as the MSB. 4. If the original number was negative, take 2’s complement of the result generated in step 3.

12 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Decimal to Binary Conversion - Examples Example 1: Let N 10 = 104. Compute N 2. Example 2: Let N 10 = -104. Compute N 2.

13 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Number Base Conversions (Continued) Hexadecimal to Binary Conversion Each hexadecimal digit corresponds to a group of 4 binary bits. Method used: Bit Grouping To convert a hexadecimal number to binary, represent each hexadecimal digit by the equivalent 4- bit binary.

14 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Hexadecimal to Binary Conversion - Example Example: Let N 16 = 1A4C2. Compute N 2.

15 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Number Base Conversions (Continued) Binary to Hexadecimal Conversion A group of 4 binary bits corresponds to a hexadecimal digit. Method used: Bit Grouping To convert a binary number to hexadecimal: Split up the binary number into 4-bit groups starting from the LSB Replace each binary group with the corresponding hexadecimal digit

16 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Binary to Hexadecimal Conversion - Example Example: Let N 2 = 1101 0011 0000 1111. Compute N 16.

17 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Arithmetic Operations Arithmetic on 2’s complement numbers is very much like the arithmetic on decimal numbers. Steps for Addition and Subtraction: 1. Express the given binary numbers as 2’s complement numbers 2. Add the numbers obtained step 1 3. Discard the end carry, if any 4. Express the result in signed-magnitude

18 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Arithmetic Operation - Example Example: Perform the following arithmetic operation using a 9-bit representation: N = 110 10 + 82 10

19 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Arithmetic Operation - Example Example: Perform the following arithmetic operation using an 8-bit representation: N = -110 10 + 82 10

20 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Status Bits Status bits are used to specify the nature of the result generated during an arithmetic or logic operation. Most common status bits are: Negative (N) bit N = 1 if the result is negative, 0 otherwise Zero (Z) bit Z = 1 if the result is zero; 0 otherwise Carry (C) bit C = 1 if there is an end carry; 0 otherwise Overflow (V) bit V = 1 if the result is out of range; 0 otherwise

21 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu The Overflow Bit If operands are too big, then the sum cannot be represented as an n-bit 2’s complement number. An overflow condition occurs when the result of an operation is out of range. We have overflow if: – signs of both operands are the same, and – sign of the sum is different. The overflow bit (V bit) detects overflow for signed numbers If the carry into the MSB bit does not equal to the carry out an overflow has occurred and V is set to 1. If they are the same, V is set to 0.

22 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Overflow - Examples We have overflow if: – signs of both operands are the same, and – sign of the sum is different. (a) 01000 (8) (b) 11000 (-8) + 01001 (9) +10111 (-9) 10001 (-15)01111 (+15) (a)N = 1Z = 0C = 0V = 1 (b) N = 0Z = 0C = 1V = 1

23 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Represents numbers with fractional components Very much like the equivalent of “scientific notation”: F x 2 E Need to represent F (fraction), E (exponent), and sign Floating Point Data Type

24 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Floating Point Data Type (Continued) IEEE 754 Floating-Point Standard (32-bits): SExponentFraction 1823 bits

25 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Floating Point Data Type (Continued) Example: How is the decimal number 8.625 represented in the floating point data type? Solution: 1. Express the decimal number in binary 8.625 = 1000.101 2. Normalize the value: 1.000101. 2 3 3. Determine the exponent field content Exponent – 127 = 3 Therefore, Exponent = 3 + 127 = 130 (Unsigned) = 10000010 4. Express the fraction using 23 bits Fraction = 00010100000000000000000 5. Sign bit = 0 because the given decimal number is a positive number So, the floating point data type for the decimal number 8.625 is 0 10000010 00010100000000000000000

26 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Floating Point Data Type (Continued) Example: What does the the floating point data type 10111111010000000000000000000000 Represent? Solution: Given floating point data type: 1 01111110 10000000000000000000000 Sign is1. This means the number is negative. Exponent field is 01111110 = 126 (decimal, unsigned) Therefore, the real exponent = 126 – 127 = -1 Fraction is represented by the 23 bits = 10000000000000000000000 Therefore, the number being represented is: -1. 10000000000000000000000. 2 -1 = - 1.5. 2 -1 = - 0.75

27 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Floating Point Data Type (Continued) Please review the following examples from the text book: Example 2.12 (Page 39 of the textbook) – 1 problem Example 2.13 (Page 39 of the textbook) – 2 problems Example 2.14 (Page 40 of the textbook) – 4 problems

28 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu ASCII Codes ASCII codes are used when the computing device has to handle letters and special symbols as well as decimal digits. ASCII - American Standard Code for Information Interchange ASCII maps 128 characters to 7-bit code.

29 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Text: ASCII Characters 00nul10dle20sp30040@50P60`70p 01soh11dc121!31141A51Q61a71q 02stx12dc222"32242B52R62b72r 03etx13dc323#33343C53S63c73s 04eot14dc424$34444D54T64d74t 05enq15nak25%35545E55U65e75u 06ack16syn26&36646F56V66f76v 07bel17etb27'37747G57W67g77w 08bs18can28(38848H58X68h78x 09ht19em29)39949I59Y69i79y 0anl1asub2a*3a:4aJ5aZ6aj7az 0bvt1besc2b+3b;4bK5b[6bk7b{ 0cnp1cfs2c,3c<4cL5c\6cl7c| 0dcr1dgs2d-3d=4dM5d]6dm7d} 0eso1ers2e.3e>4eN5e^6en7e~ 0fsi1fus2f/3f?4fO5f_6fo7fdel

30 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu ASCII Codes & Parity Bit ASCII is a 7-bit representation Parity bit is added to the ASCII, as the most significant bit, to give the ASCII an 8-bit representation Two types of parity – Even & Odd Even Parity – The total number of 1s in the 8-bit representation is an even number Odd Parity – The total number of 1s in the 8-bit representation is an odd number


Download ppt "S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems."

Similar presentations


Ads by Google