CS 2130 Lecture 23 Data Types Scalar Types • • • • • • • int float char long short boolean enums* Scalar Types • • • • • • • • int float char long short double boolean enums* Integer Numbers • *Some languages implement enumerated types in a more restrictive fashion than C Some languages implement enumerated types in a more restrictive fashion than C enum {FOO, BAR, BAZ, BLARG}; 0 1 2 3 These become only allowed values C Types • C "thinks" of 3 basic types: char, int and float Typical Size* Type Comments 1 char Really just a 1 byte int 2 short 4 int at least as long as a short 8 long at least as long as an int 4 float 8 double *in bytes which are normally but not necessarily 8 bits DON'T FORGET! •signed •unsigned 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? Depends! • Unsigned • Signed • 2N • 2N-1 or 2N • Numbers range from 0 to 2N-1 • Depends on representation – – – – Signed Magnitude One's Complement Two's Complement Biased/Excess Notation Ranges Unsigned Signed Magnitude One's Complement Two's Complement Biased/ Excess Notation (Bias = B) From 0 To 2N - 1 -(2N-1 - 1) +(2N-1 - 1) -(2N-1 - 1) +(2N-1 - 1) -(2N-1) +(2N-1 - 1) -B 2N - 1 - B What? N = 3 Binary Unsigned Signed One's Two's Excess Excess Mag Comp Comp 3 4 000 001 010 011 100 101 110 111 Number Stored 0 1 2 3 4 5 6 7 0 1 2 3 -0 -1 -2 -3 0 1 2 3 -3 -2 -1 -0 Number Represented 0 1 2 3 -4 -3 -2 -1 -3 -2 -1 0 1 2 3 4 -4 -3 -2 -1 0 1 2 3 N = 4 Number Represented Binary Unsigned Signed One's Two's Excess Excess Mag Comp Comp 7 8 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 -0 -1 -2 -3 -4 -5 -6 -7 0 1 2 3 4 5 6 7 -7 -6 -5 -4 -3 -2 -1 -0 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 N = 8 Binary 00000000 00000001 00000010 ... 01111111 10000000 10000001 ... 11111110 11111111 Unsigned Signed One's Two's Excess Excess Mag Comp Comp 127 128 0 1 2 ... 127 128 129 ... 254 255 0 1 2 ... 127 -0 -1 ... -126 -127 0 1 2 ... 127 -127 -126 ... -1 -0 0 1 2 ... 127 -128 -127 ... -2 -1 -127 -126 -125 ... 0 1 2 ... 127 128 -128 -127 -126 ... -1 0 1 ... 126 127 N = 32 Binary 00000000000000000000000000000000 00000000000000000000000000000001 00000000000000000000000000000010 ... 01111111111111111111111111111111 10000000000000000000000000000000 10000000000000000000000000000001 ... 11111111111111111111111111111110 11111111111111111111111111111111 Hex 0 1 2 ... 7FFFFFFF 80000000 80000001 ... FFFFFFFE FFFFFFFF Unsigned 0 1 2 ... 2147483647 2147483648 2147483649 ... 4294967294 4294967295 N = 32 Hex 0 1 2 ... 7FFFFFFF 80000000 80000001 ... FFFFFFFE FFFFFFFF Unsigned Signed Mag One's Comp Two's Comp 0 0 0 0 1 1 1 1 2 2 2 2 ... ... ... ... 2147483647 2147483647 2147483647 2147483647 2147483648 -0 -2147483647 -2147483648 2147483649 -1 -2147483646 -2147483647 ... ... ... ... 4294967294 -2147483646 -1 -2 4294967295 -2147483647 -0 -1 Where do bits come from? Numeric Bits 14,000' Bitaba Tree Andes Mountains Character Bits NOT So what's the difference... • Between a char 1 and an int 1 – about 48? • Between the ip address 128.43.56.102 and 0x80263866 and -2,144,978,842 and 2,149,988,454 • The letter A and an int 65 What about these? 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 Don't forget... ASCII Table (7-bit) Decimal Octal ----------000 000 001 001 002 002 003 003 004 004 005 005 006 006 007 007 008 010 009 011 010 012 011 013 012 014 013 015 014 016 015 017 016 020 017 021 018 022 019 023 020 024 Hex --000 001 002 003 004 005 006 007 008 009 00A 00B 00C 00D 00E 00F 010 011 012 013 014 Binary -----00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001010 00001011 00001100 00001101 00001110 00001111 00010000 00010001 00010010 00010011 00010100 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) LF (Line Feed) VT (Vertical Tab) FF (Form Feed) CR (Carriage Return) SO (Serial In)(Shift Out) SI (Serial Out)(Shift Out) DLE (Data Link Escape) DC1 (XON) (Device Control 1) DC2 (Device Control 2) DC3 (XOFF)(Device Control 3) DC4 (Device Control 4) Why two codes? Don't forget... ASCII Table (7-bit) Decimal Octal ----------021 025 022 026 023 027 024 030 025 031 026 032 027 033 028 034 029 035 030 036 031 037 032 040 033 041 034 042 035 043 036 044 037 045 038 046 039 047 040 050 Hex --015 016 017 018 019 01A 01B 01C 01D 01E 01F 020 021 022 023 024 025 026 027 028 Binary -----00010101 00010110 00010111 00011000 00011001 00011010 00011011 00011100 00011101 00011110 00011111 00100000 00100001 00100010 00100011 00100100 00100101 00100110 00100111 00101000 Value ----NAK SYN ETB CAN EM SUB ESC FS GS RS US SP ! " # $ % & ' ( (Negative Acknowledgement) (Synchronous Idle) (End of Trans. Block) (Cancel) (End of Medium) (Substitute) (Escape) (File Separator) (Group Separator) (Request to Send)(Record Separator) (Unit Separator) (Space) Don't forget... ASCII Table (7-bit) Decimal Octal ----------041 051 042 052 043 053 044 054 045 055 046 056 047 057 048 060 049 061 050 062 051 063 052 064 053 065 054 066 055 067 056 070 057 071 058 072 059 073 060 074 Hex --029 02A 02B 02C 02D 02E 02F 030 031 032 033 034 035 036 037 038 039 03A 03B 03C Binary -----00101001 00101010 00101011 00101100 00101101 00101110 00101111 00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000 00111001 00111010 00111011 00111100 Value ----) * + , . / 0 1 2 3 4 5 6 7 8 9 : ; < Don't forget... ASCII Table (7-bit) Decimal Octal ----------061 075 062 076 063 077 064 100 065 101 066 102 067 103 068 104 069 105 070 106 071 107 072 110 073 111 074 112 075 113 076 114 077 115 078 116 079 117 080 120 Hex --03D 03E 03F 040 041 042 043 044 045 046 047 048 049 04A 04B 04C 04D 04E 04F 050 Binary -----00111101 00111110 00111111 01000000 01000001 01000010 01000011 01000100 01000101 01000110 01000111 01001000 01001001 01001010 01001011 01001100 01001101 01001110 01001111 01010000 Value ----= > ? @ A B C D E F G H I J K L M N O P Don't forget... ASCII Table (7-bit) Decimal Octal ----------081 121 082 122 083 123 084 124 085 125 086 126 087 127 088 130 089 131 090 132 091 133 092 134 093 135 094 136 095 137 096 140 097 141 098 142 099 143 100 144 Hex --051 052 053 054 055 056 057 058 059 05A 05B 05C 05D 05E 05F 060 061 062 063 064 Binary -----01010001 01010010 01010011 01010100 01010101 01010110 01011111 01011000 01011001 01011010 01011011 01011100 01011101 01011110 01011111 01100000 01100001 01100010 01100011 01100100 Value ----Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d Don't forget... ASCII Table (7-bit) Decimal Octal ----------101 145 102 146 103 147 104 150 105 151 106 152 107 153 108 154 109 155 110 156 111 157 112 160 113 161 114 162 115 163 116 164 117 165 118 166 119 167 120 170 Hex --065 066 067 068 069 06A 06B 06C 06D 06E 06F 070 071 072 073 074 075 076 077 078 Binary -----01100101 01100110 01100111 01101000 01101001 01101010 01101011 01101100 01101101 01101110 01101111 01110000 01110001 01110010 01110011 01110100 01110101 01110110 01110111 01111000 Value ----e f g h i j k l m n o p q r s t u v w x Don't forget... ASCII Table (7-bit) Decimal Octal ----------121 171 122 172 123 173 124 174 125 175 126 176 127 177 Hex --079 07A 07B 07C 07D 07E 07F Binary -----01111001 01111010 01111011 01111100 01111101 01111110 01111111 Value ----y z { | } ~ DEL Why is this at the end? Unsigned • Add 89 and 205 • Step 1: Convert 89 and 205 to binary Converting to Binary • Use dc – See man dc • Use some other calculator • Learn how to do it by hand !!! Old Stuff http://www.pgh.net/~newcomer/hp16c.htm Converting to Binary 89 44 22 11 5 2 1 0 odd even even odd odd even odd even 1 0 0 1 1 0 1 0 01011001 Converting to Binary 205 102 51 25 12 6 3 1 odd even odd odd even even odd odd 1 0 1 1 0 0 1 1 11001101 Why does this work? 47 23 11 5 2 1 0 0 odd odd odd odd even odd even even 1 1 1 1 0 1 0 0 48 24 12 6 3 1 0 0 00101111 even even even even odd odd even even 0 0 0 0 1 1 0 0 00110000 Unsigned • Add 89 and 205 01011001 11001101 100100110 Significance of this? Result = ??? + 0 1 0 0 1 1 1 10 What happened? • Overflow (carry out from high order bit) means that the result is bigger than the allowed number of bits 89 + 205 = 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? 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? Signed Magnitude? • Add these two numbers 01011001 11001101 • What does this represent? Signed Magnitude? • What does this represent? 010110012 = 8910 110011012 = -7710 • How do we do the problem in decimal arithmetic? • Note: This is not subtraction. Addition of signed numbers – Recall 3rd Grade Signed Magnitude? • What does this represent? 010110012 = 8910 110011012 = -7710 0 If signs are different sign of result will be sign of larger operand Signed Magnitude? • What does this represent? 010110012 = 8910 110011012 = -7710 000011002 = 1210 • What are the implications of this operation? One's Complement • Easy • Create binary representation of negative number, ignoring sign • Flip all bits • Example -94: 01011110 • Flip Bits 10100001 One's Complement? • What does this represent? 01011001 11001101 One's Complement? • What does this represent? 010110012 = 8910 110011012 = -5010 One's Complement? • What does this represent? 010110012 = 8910 110011012 = -5010 3910 • Suppose we just add? One's Complement? • What does this represent? 010110012 = 8910 110011012 = -5010 1001001102 3910 001001102 = 3810 • So we ignore the overflow and have to add 1 to get the right answer? • Is this useful? Two's Complement • Calculate One's Complement • Add 1 • Example -94: 01011110 • Produce One's Complement 10100001 • Add 1 10100010 • Why do we do this? • Hardware Reasons Why does it work? • Consider decimal numbers. • Suppose we have 3 digits available 000 - 999 • We can use 10's complement! • Example -94 • First form 9's complement of 094 905 • Add 1 906 • But what is this? 906 - 1000 = ? • So what? Add 107 and -94 • Decimal Form 107 -94 • Rewrite as 107 906 - 1000 • Add 1013 - 1000 13 Two's Complement? • What does this represent? 010110012 110011012 Two's Complement? • What's the decimal answer? 010110012 = 8910 110011012 = -5110 Two's Complement? • What happens if we add the binary numbers? 010110012 = 8910 110011012 = -5110 3810 Two's Complement? • The eight bit result is correct 010110012 = 8910 110011012 = -5110 1001001102 3810 3810 Two's Complement? • Overflow? 010110012 = 8910 110011012 = -5110 1001001102 3810 Significance of this? • What's the rule? Consider • 1 digit decimal numbers – Positive numbers: 0 1 2 3 4 – Negative numbers: 9 8 7 6 5 (-1 -2 -3 -4 -5) 2 2 4 2 4 6 -2 4 2 2 -4 -2 -2 -2 -4 -2 -6 -4 2 2 4 2 4 6 8 4 12 2 6 8 8 8 6 8 14 16 Bad Ok Ok Bad Recall N = 3 Binary Unsigned Signed One's Two's Excess Excess Mag Comp Comp 3 4 000 001 010 011 100 101 110 111 Number Stored 0 1 2 3 4 5 6 7 0 1 2 3 -0 -1 -2 -3 0 1 2 3 -3 -2 -1 -0 Number Represented 0 1 2 3 -4 -3 -2 -1 -3 -2 -1 0 1 2 3 4 -4 -3 -2 -1 0 1 2 3 Consider • 3 bit numbers: – Max allowed 3 (011) – Min allowed -4 (100) 2 3 5 -2 3 1 2 -3 -1 -2 -3 -5 010 011 0101 Bad 110 011 1001 Ok 010 101 0111 Ok 110 101 1011 Bad If carry in to sign bit != carry out of sign bit: Overflow 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 958 - 1000 • If we want to add 98 and -42 (Should get 56) 98 0 958 - 1000 1056 - 1000 = 56 Think about forming the complement 100000 42653 57347 Another way: 42653 Write down 9's complement 57346 Then add 1 57347 Binary works the same way! What's the 2's complement of 56? Assume we have an 8 bit machine 56 = 111000 100000000 -111000 11001000 Easier way Start with: Take 1's complement: Add 1: 00111000 11000111 11001000 Binary works the same way! 2 3 5 010-0 011-0 101 -3 Bad -2 3 1 110-1000 0110 1001-1000 1 Ok 2 -3 -1 0100 101-1000 111-1000 -1 Ok -2 -3 -5 110 -1000 101 -1000 1011-10000 -5 Bad Excess Notation • • • • • • • Need to define bias Add bias to number to be represented Example: Bias = 127 Number to be represented: 42 Calculate: 42 + 127 = 169 Convert to binary unsigned magnitude 10101001 • To decode, subtract bias • Calculate 169 - 127 = 42 We will see Excess Notation again with floating point 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 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. BCD • Example: Decimal 4045 • Binary: 1111 1100 1101 (12 digits) • BCD: 0100 0000 0100 0101 (16 digits) • BCD is less efficient from the standpoint of number of bits required to store a number. BCD • Special routines are required to do math Example: 0011 3 0100 4 0111 7 (less 10...okay) 0111 1000 1111 0110 1 0101 7 8 15 (>9 add 6) 5 (with carry) BCD • Typical approaches to negative numbers use 9 and 10's complement arithmetic • BCD numbers are implemented in COBOL Questions? 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 Questions?