Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication."— Presentation transcript:

1 © Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication

2 © Janice Regan, CMPT 128, 2013 1 Constants and Variables  Each constant or variable represents a specific item in the problem the program solves and is associated with  a memory cell used to hold its value  a unique identifier or name  a data type  The value of a constant does not change during execution of the program  The value of a variable may be changed during the execution of a program.

3 © Janice Regan, CMPT 128, 2013 2 Types of Constants and Variables  A Data Type is  A set of values plus a set of operations on those values  A crucial concept on modern programming  The data type of a variable or constant also determines how the variable’s value will be represented in memory  Variables of several types can have numerical values  Variables of other types have values that are characters, or logical values.

4 © Janice Regan, CMPT 128, 2013 3 Types with Numerical Values  Two classes of numerical values  Integer and floating point  Integer values are whole numbers  No fractional part  1, 12354, 68, -123  C++ Types: int, unsigned int, long int, short int, long unsigned int, …

5 © Janice Regan, CMPT 128, 2013 4 Types with Numerical Values  Two classes of numerical values  Integer and floating point  Floating point values have a fractional part  987.4, -0.332, 0.123, 3.14159  C++ types: float, double, long double  Integers and floating point numbers are represented differently inside the computer

6 © Janice Regan, CMPT 128, 2013 5 Positive Integers  Positive Integers are represented by a string of binary digits (0s and 1s)  C++ type unsigned int long unsigned int  long unsigned int variables usually represent numbers with 2x as many binary digits as unsigned int variables N Binary Digits

7 © Janice Regan, CMPT 128, 2013 6 Representing positive Integers  An unsigned integer has particular values. For a positive integer represented by N binary digits the possible values are 0 <= value <= 2 N -1. 255 N Binary Digits 11111111 2727 2626 2525 2424 23232 2121 2020 1286432168421

8 Examples © Janice Regan, CMPT 128, 2013 7 00100011 2727 2626 2525 2424 23232 2121 2020 003200021 10101011 2727 2626 2525 2424 23232 2121 2020 12803208021 35 171

9 Converting to binary  111  128 too large so 111 - 64 = 47  47 - 32 = 15  16 too large so 15 – 8 = 7  7 – 4 = 3  3 – 2 = 1 0 1 1 0 1 1 1 1 © Janice Regan, CMPT 128, 2013 8

10 Your turn  10101010  10101111  58  259 © Janice Regan, CMPT 128, 2013 9

11 10 Signed Integers  Signed Integers are also represented by a string of binary digits (0s and 1s)  C++ type int long int  long int variables usually represent numbers with 2x as many binary digits as int variables N Binary Digits Sign bit

12 © Janice Regan, CMPT 128, 2013 11 Representing signed Integers  An signed integer has particular values. For a positive integer represented by N binary digits the possible values are 2 N-1 -1 <= value <= 2 N-1 -1. +/- 127 N -1 Binary Digits 1111111 +/-2626 2525 2424 23232 2121 2020 6432168421 Sign bit

13 Problems  Which value 1 or 0 denotes the positive or negative sign.  Difficult to define addition and subtraction to be efficient on typical computer hardware for either choice  More than one value of 0  +0 00000000  - 0 10000000 © Janice Regan, CMPT 128, 2013 12

14 Problems  Difficult to define addition and subtraction to be efficient on typical computer hardware for either choice 1 1 0 1 (-5) 1 1 0 1 0 1 0 (2) 0 1 0 1 1 1 1 (-7) (-0 ) 1 0 0 0  OR or ADD AND © Janice Regan, CMPT 128, 2013 13

15 © Janice Regan, CMPT 128, 2013 14 Ones Complement Integer Representation  Integers are represented by a string of binary digits. signed integer  Same as unsigned if first bit is 0  If first bit is 1 need to f lip all the bits  Flip all the bits 10110001 (49) becomes 01001110  01001110 = 2 6 + 2 3 +2 2 +2 1 = 64+8+4+2 =78  So 10110001 represents -78 + N Binary Digits Sign bit

16 © Janice Regan, CMPT 128, 2013 15 Ones Complement Advantages / problems  Advantage  Addition is now more efficient  Disadvantage  There are still two representations of 0 +0 00000000 - 0 11111111 +

17 Examples: ones complement © Janice Regan, CMPT 128, 2013 16 00100011 2727 2626 2525 2424 23232 2121 2020 0100011 03200021 10101011 2727 2626 2525 2424 23232 2121 2020 1010100 -640160400 35 -84 Remember if first digit is1 flip bits

18 Examples: ones complement © Janice Regan, CMPT 128, 2013 17 01001111 2727 2626 2525 2424 23232 2121 2020 1001111 64008421 11001101 2727 2626 2525 2424 23232 2121 2020 0110010 -032160020 79 -50 Remember if first digit is 1 flip bits

19 Decimal to 1s complement  -49 (number < 0)  Express 49 in 8 bit binary 32+16+1 00110001  Flip the bits 11001110 © Janice Regan, CMPT 128, 2013 18

20 Decimal to 1s complement  111 (number > 0)  Express 111 in 8 bit binary 32+16+1 01101111 © Janice Regan, CMPT 128, 2013 19

21 Your turn  10101010  11011010  120  -59 © Janice Regan, CMPT 128, 2013 20

22 Ones complement addition 0 0 1 1 1 0 0 157 0 1 0 0 0 0 0 1 65 0 1 1 1 1 0 1 0 122 0 0 1 1 1 0 1 159 1 0 0 0 0 1 1 1-120 1 1 0 0 0 0 1 0-61 © Janice Regan, CMPT 128, 2013 21

23 Ones complement addition 1 0 1 1 1 0 0 1-70 1 1 1 0 0 0 0 1 -30 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 1 1-100 © Janice Regan, CMPT 128, 2013 22

24 Ones complement addition 0 1 1 1 1 0 0 1121 1 1 0 0 0 0 1 1 -60 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 161 © Janice Regan, CMPT 128, 2013 23

25 Problems? overflow 0 1 1 1 1 0 0 1121 0 1 0 0 0 0 0 0 64 1 0 1 1 1 1 0 0 -67 ????  121 + 64 = 185  Largest integer that can be represented is 127 © Janice Regan, CMPT 128, 2013 24

26 Problems? overflow 1 0 1 1 1 0 0 1-70 1 1 0 0 0 0 0 0 -63 1 0 1 1 1 1 0 0 1 0 1 1 1 1 0 1 0122  -70 + -63 = -133  Smallest integer that can be represented is -127 © Janice Regan, CMPT 128, 2013 25

27 Your turn again  12 + 55  -59 + - 12  17 - 33 © Janice Regan, CMPT 128, 2013 26

28 © Janice Regan, CMPT 128, 2013 27 Twos Complement Integer Representation  Integers are represented by a string of binary digits. signed integer  Same as 1s unsigned if first bit is 0  If first bit is 1 need to  Flip all the bits 10110001 becomes 01001110  Add 1 01001110 becomes 01001111  01001111 = 26 + 23 +22+21+20 = 64+8+4+2+1 =79  So 1011001 represents -79  No longer two representations of 0 + N Binary Digits Sign bit

29 Examples: twos complement © Janice Regan, CMPT 128, 2013 28 00100011 2727 2626 2525 2424 23232 2121 2020 0100011 03200021 10101011 2727 2626 2525 2424 23232 2121 2020 1010100 1010101 -640160401 35 -85 Remember if first digit is 1 flip bits then add 1

30 Examples: twos complement © Janice Regan, CMPT 128, 2013 29 01001111 2727 2626 2525 2424 23232 2121 2020 1001111 64008421 11001100 2727 2626 2525 2424 23232 2121 2020 0110011 0110100 -032160400 79 -52 Remember if first digit is 1 flip bits then add 1

31 Decimal to 2s complement  -72 ( number < 0)  Express 72 in 8 bit binary 64+8 01001000  Flip the bits 10110111  Add 1 10111000 © Janice Regan, CMPT 128, 2013 30

32 Decimal to 2s complement  35 (number > 0 )  Express 72 in 8 bit binary 32+2+1 00100011 © Janice Regan, CMPT 128, 2013 31

33 Your turn  10101010  11011010  120  -59 © Janice Regan, CMPT 128, 2013 32

34 Compare representations BitsUnsigned+ / -1’s comp2's comp 000000000000 000000011111 000000102222 01111110126 01111111127 100000001280-127−128 10000001129-126−127 10000010130-2-125−126 11111110254-126−2 11111111255-1270−1 © Janice Regan, CMPT 128, 2013 33

35 Twos complement addition 0 0 1 1 1 0 0 056 0 1 0 0 0 0 0 0 64 0 1 1 1 1 0 0 0 120 0 0 1 1 1 0 1 159 1 0 0 0 0 1 1 1-121 1 1 0 0 0 0 1 0-62 © Janice Regan, CMPT 128, 2013 34

36 Twos complement addition 0 1 1 0 1 1 1 0110 1 1 0 1 1 0 1 1 -37 1 0 1 0 0 1 0 0 1 73 1 0 1 1 1 0 1 1-69 1 1 0 1 0 1 0 1 -43 1 1 0 0 1 0 0 0 0 -112 © Janice Regan, CMPT 128, 2013 35 Throw away

37 Twos complement overflow 0 1 1 0 1 1 1 0110 0 1 0 1 1 0 1 1 91 1 1 0 0 1 0 0 1 -55 1 0 0 1 1 1 1 0-96 1 1 0 0 0 1 0 1 -59 1 0 1 1 0 0 0 1 1 99 © Janice Regan, CMPT 128, 2013 36

38 Overflow: 2s complement  If the sum of two positive numbers is negative, overflow has occurred  If the sum of two negative numbers is positive, overflow has occurred  Overflow does not occur adding a positive number and a negative number.  Overflow happens when there is carry over into the sign bit © Janice Regan, CMPT 128, 2013 37

39 Your turn again  -66  32  48 – 64  57 + 22 © Janice Regan, CMPT 128, 2013 38

40 2s complement  Multiplication is repeated 2s complement addition  Division is repeated 2s complement subtraction © Janice Regan, CMPT 128, 2013 39

41 If you are interested  Multiplication is done by repeated addition  Some examples of multiplying twos complement numbers are found on the following slides © Janice Regan, CMPT 128, 2013 40

42 2s complement multiplication  14 * 7  00000000 00001110 14  00000000 00000111 7  00000000 00001110  00000000 00011100.  00000000 00111000  00000000 0110001098 © Janice Regan, CMPT 128, 2013 41

43 2s complement multiplication  12 * 9  11111111 11110100 -12  00000000 00001001 9  11111111 11110100  11111111 10100000  11111111 10010100108 © Janice Regan, CMPT 128, 2013 42

44 2s complement multiplication  -15 * 25  11111111 11110001 -15  00000000 0001100125  11111111 11110001  11111111 10001000  11111111 00010000  11111110 10001001 -375 © Janice Regan, CMPT 128, 2013 43

45 2s complement multiplication  55 * 27  00000000 00110111 55  00000000 0001101127  00000000 00110111  00000000 01101110  00000001 10111000  00000011 01110000  00000101 110011011485 © Janice Regan, CMPT 128, 2013 44


Download ppt "© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication."

Similar presentations


Ads by Google