Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2130 Lecture 23 Data Types.

Similar presentations


Presentation on theme: "CS 2130 Lecture 23 Data Types."— Presentation transcript:

1 CS 2130 Lecture 23 Data Types

2 Scalar Types int float char long short boolean enums*

3 Scalar Types int float char long short double boolean Integer Numbers
enums* *Some languages implement enumerated types in a more restrictive fashion than C Integer Numbers

4 Some languages implement enumerated types in a more restrictive fashion than C
enum {FOO, BAR, BAZ, BLARG}; These become only allowed values

5 C Types C "thinks" of 3 basic types: char, int and float Typical Size*
Comments 1 char Really just a 1 byte int 2 4 int at least as long as a short 8 at least as long as an int float double long short *in bytes which are normally but not necessarily 8 bits

6 DON'T FORGET! signed unsigned

7 More Bits, More Precision
int (4)...long (8) Twice as long, twice as precise Given an N bit number, how many different numbers can be represented? Your final answer?

8 Depends! Unsigned 2N Numbers range from 0 to 2N-1 Signed 2N-1 or 2N
Depends on representation Signed Magnitude One's Complement Two's Complement Biased/Excess Notation

9 Ranges Unsigned Signed Magnitude One's Complement Two's Complement
Biased/ Excess Notation (Bias = B) From -(2N-1 - 1) -(2N-1) -B To 2N - 1 +(2N-1 - 1) 2N B

10 What? N = 3 Binary 000 001 010 011 100 101 110 111 Unsigned 1 2 3 4 5
1 2 3 4 5 6 7 Signed Mag 1 2 3 -0 -1 -2 -3 One's Comp 1 2 3 -3 -2 -1 -0 Two's Comp 1 2 3 -4 -3 -2 -1 Excess 3 -3 -2 -1 1 2 4 Excess 4 -4 -3 -2 -1 1 2 3 Number Stored Number Represented

11 N = 4 Number Represented Unsigned 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Signed Mag -0 -1 -2 -3 -4 -5 -6 -7 One's Comp Two's -8 Excess N = 4 Number Represented

12 N = 8 Binary ... Unsigned 1 2 ... 127 128 129 254 255 Signed Mag 1 2 ... 127 -0 -1 -126 -127 One's Comp 1 2 ... 127 -127 -126 -1 -0 Two's Comp 1 2 ... 127 -128 -127 -2 -1 Excess 127 -127 -126 -125 ... 1 2 128 Excess 128 -128 -127 -126 ... -1 1 126 127

13 N = 32 Binary ... Hex 1 2 ... 7FFFFFFF FFFFFFFE FFFFFFFF Unsigned 1 2 ...

14 N = 32 Hex 1 2 ... 7FFFFFFF 80000000 80000001 FFFFFFFE FFFFFFFF
1 2 ... 7FFFFFFF FFFFFFFE FFFFFFFF Unsigned 1 2 ... Signed Mag 1 2 ... -0 -1 One's Comp 1 2 ... -1 -0 Two's Comp 1 2 ... -2 -1

15 Where do bits come from? Bitaba Tree Andes Mountains Numeric Bits
14,000' Numeric Bits Character Bits

16 NOT

17 So what's the difference...
Between a char 1 and an int 1 about 48? Between the ip address and 0x and -2,144,978,842 and 2,149,988,454 The letter A and an int 65

18 What about these?

19 Key Point Data is stored internally as binary numbers with no meaning to the computer. All meaning is assigned by programmer using programming languages and other tools. Example: char c = 65; printf(" %d \n", c); printf(" %c \n", c); Output: 65 A

20 Don't forget... Why two codes? ASCII Table (7-bit)
Decimal Octal Hex Binary Value NUL (Null char.) SOH (Start of Header) STX (Start of Text) ETX (End of Text) EOT (End of Transmission) ENQ (Enquiry) ACK (Acknowledgment) BEL (Bell) BS (Backspace) HT (Horizontal Tab) A LF (Line Feed) B VT (Vertical Tab) C FF (Form Feed) D CR (Carriage Return) E SO (Serial In)(Shift Out) F SI (Serial Out)(Shift Out) DLE (Data Link Escape) DC1 (XON) (Device Control 1) DC (Device Control 2) DC3 (XOFF)(Device Control 3) DC (Device Control 4) Why two codes?

21 Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value
NAK (Negative Acknowledgement) SYN (Synchronous Idle) ETB (End of Trans. Block) CAN (Cancel) EM (End of Medium) A SUB (Substitute) B ESC (Escape) C FS (File Separator) D GS (Group Separator) E RS (Request to Send)(Record Separator) F US (Unit Separator) SP (Space) ! " # $ % & ' (

22 Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value
) A * B C , D E F / A : B ; C <

23 Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value
D = E > F ? @ A B C D E F G H I A J B K C L D M E N F O P

24 Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value
Q R S T U V W X Y A Z B [ C \ D ] E ^ F _ ` a b c d

25 Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value
e f g h i A j B k C l D m E n F o p q r s t u v w x

26 Don't forget... Why is this at the end? ASCII Table (7-bit)
Decimal Octal Hex Binary Value y A z B { C | D } E ~ F DEL Why is this at the end?

27 Unsigned Add 89 and 205 Step 1: Convert 89 and 205 to binary

28 Converting to Binary Use dc Use some other calculator
See man dc Use some other calculator Learn how to do it by hand !!!

29 Old Stuff

30 Converting to Binary 89 odd 1 44 even 0 22 even 0 11 odd 1 5 odd 1

31 Converting to Binary 205 odd 1 102 even 0 51 odd 1 25 odd 1 12 even 0

32 Why does this work? 47 odd 1 23 odd 1 11 odd 1 5 odd 1 2 even 0
48 even 0 24 even 0 12 even 0 6 even 0 3 odd 1 1 odd 1 0 even 0

33 Unsigned Add 89 and 205 01011001 11001101 100100110 Result = ??? + 1 1
1 1 1 1 10 Significance of this?

34 What happened? Overflow (carry out from high order bit) means that the result is bigger than the allowed number of bits = 294 Assumption for this discussion: Two N bit numbers may be added together and overflow can be detected BTW: When adding two N bit numbers together...what is the maximum number of bits the result can have? Can we do subtraction?

35 Signed Magnitude Both positive and negative zero
Equal number of positives and negatives Easy to interpret First bit is the sign Remaining bits are number Sounds ideal?

36 Signed Magnitude? Add these two numbers 01011001 11001101
What does this represent?

37 Signed Magnitude? What does this represent? 010110012 = 8910
= 8910 = -7710 How do we do the problem in decimal arithmetic? Note: This is not subtraction. Addition of signed numbers Recall 3rd Grade

38 Signed Magnitude? What does this represent? 010110012 = 8910
= 8910 = -7710 If signs are different sign of result will be sign of larger operand

39 Signed Magnitude? What does this represent? 010110012 = 8910
= 8910 = -7710 = 1210 What are the implications of this operation?

40 One's Complement Easy Create binary representation of negative number, ignoring sign Flip all bits Example -94: Flip Bits

41 One's Complement? What does this represent?

42 One's Complement? What does this represent? 010110012 = 8910
= 8910 = -5010

43 One's Complement? What does this represent? 010110012 = 8910
= 8910 = -5010 3910 Suppose we just add?

44 One's Complement? What does this represent? 010110012 = 8910
= 8910 = -5010 = 3810 So we ignore the overflow and have to add 1 to get the right answer? Is this useful?

45 Two's Complement Calculate One's Complement Add 1 Example -94:
Produce One's Complement Why do we do this? Hardware Reasons

46 Why does it work? Consider decimal numbers.
Suppose we have 3 digits available We can use 10's complement! Example -94 First form 9's complement of 094 905 Add 1 906 But what is this? = ? So what?

47 Add 107 and -94 Decimal Form 107 -94 Rewrite as 906 - 1000 Add
13

48 Two's Complement? What does this represent?

49 Two's Complement? What's the decimal answer? 010110012 = 8910
= 8910 = -5110

50 Two's Complement? What happens if we add the binary numbers?
= 8910 = -5110 3810

51 Two's Complement? The eight bit result is correct 010110012 = 8910
= 8910 = -5110 3810

52 Two's Complement? Overflow? 010110012 = 8910 110011012 = -5110
= 8910 = -5110 What's the rule? Significance of this?

53 Consider 1 digit decimal numbers 2 2 -2 2 -2 -2 2 4 4 -4 -4 -2
Positive numbers: Negative numbers: ( ) Bad Ok Ok Bad

54 Recall N = 3 Binary 000 001 010 011 100 101 110 111 Unsigned 1 2 3 4 5
1 2 3 4 5 6 7 Signed Mag 1 2 3 -0 -1 -2 -3 One's Comp 1 2 3 -3 -2 -1 -0 Two's Comp 1 2 3 -4 -3 -2 -1 Excess 3 -3 -2 -1 1 2 4 Excess 4 -4 -3 -2 -1 1 2 3 Number Stored Number Represented

55 Consider 3 bit numbers: 2 -2 2 -2 3 3 -3 -3 5 1 -1 -5 010 110 010 110
Max allowed 3 (011) Min allowed -4 (100) Bad Ok Ok Bad If carry in to sign bit != carry out of sign bit: Overflow

56 But why? Back to decimal land...
Using complement notation to represent negative numbers looks like this: Assume 3 decimal digits available (no sign) Want -42 Subtract 42 from 1000 = 958 (Ten's Complement) So we get If we want to add 98 and -42 (Should get 56) = 56

57 Think about forming the complement
100000 42653 57347 Another way: Write down 9's complement 57346 Then add 1

58 Binary works the same way!
What's the 2's complement of 56? Assume we have an 8 bit machine 56 = Easier way Start with: Take 1's complement: Add 1:

59 Binary works the same way!
Bad Ok Ok Bad

60 Excess Notation Need to define bias
Add bias to number to be represented Example: Bias = 127 Number to be represented: 42 Calculate: = 169 Convert to binary unsigned magnitude To decode, subtract bias Calculate = 42 We will see Excess Notation again with floating point

61 Binary Coded Decimal For certain applications such as representing currency amounts, numbers must be exact. The problems of representing decimal floating point numbers with binary representations cannot be tolerated Solution: BCD or Binary Coded Decimal

62 BCD Encode each decimal digit as a sequence of binary digits 0: 0000
1: 0001 2: 0010 3: 0011 4: 0100 5: 0101 6: 0110 7: 0111 8: 1000 9: 1001 Note: 1010, 1011, 1100, 1101, 1110 and 1111 not used.

63 BCD Example: Decimal 4045 Binary: 1111 1100 1101 (12 digits)
BCD: (16 digits) BCD is less efficient from the standpoint of number of bits required to store a number.

64 BCD Special routines are required to do math Example: 0011 3 0100 4
(less 10...okay) (>9 add 6) 0110 (with carry)

65 BCD Typical approaches to negative numbers use 9 and 10's complement arithmetic BCD numbers are implemented in COBOL

66 Questions?

67 Key Ideas The computer just manipulates bits.
The same bit pattern can mean different things based on programmer and software Different representations are used internally based on considerations such as Cost Speed Typical time vs space tradeoffs. YOU REALLY NEED TO UNDERSTAND THIS STUFF

68 Questions?

69


Download ppt "CS 2130 Lecture 23 Data Types."

Similar presentations


Ads by Google