CS 2130 Lecture 23 Data Types.

Slides:



Advertisements
Similar presentations
ICS312 Set 2 Representation of Numbers and Characters.
Advertisements

HEXADECIMAL NUMBERS Code
Data Representation Computer Organization &
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Data Representation COE 205
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Data Representation ICS 233
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Simple Data Type Representation and conversion of numbers
1 Data Representation Computer Organization Prof. H. Yoon DATA REPRESENTATION Data Types Complements Fixed Point Representations Floating Point Representations.
The character data type char
Computers Organization & Assembly Language
BIOS1 Basic Input Output System BIOS BIOS refers to a set of procedures or functions that enable the programmer have access to the hardware of the computer.
Digital Design: From Gates to Intelligent Machines
Decimal Binary Octal Hex
IT253: Computer Organization
Computer Architecture
Dept. of Computer Science Engineering Islamic Azad University of Mashhad 1 DATA REPRESENTATION Dept. of Computer Science Engineering Islamic Azad University.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
Lecture 4: Number Systems (Chapter 3) (1) Data TypesSection3-1 (2) ComplementsSection3-2 (3) Fixed Point RepresentationsSection3-3 (4) Floating Point RepresentationsSection3-4.
CS151 Introduction to Digital Design
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
1 Information Representation in Computer Lecture Nine.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Systems Architecture, Fourth Edition 1 Data Representation Chapter 3.
CEC 220 Digital Circuit Design Binary Codes Mon, Aug 31 CEC 220 Digital Circuit Design Slide 1 of 14.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
Programming for GCSE Topic 2.2: Binary Representation T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
PRIMITIVE TYPES IN JAVA Primitive Types Operations on Primitive Types.
Data Representation COE 308 Computer Architecture
Bits, Data Types, and Operations
Chapter 2 Bits, Data Types, and Operations
Machine level representation of data Character representation
Data Representation ICS 233
Lec 3: Data Representation
Characters Lesson Outline
Data Representation.
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Chapter 2 Data Types and Representations
Javascript, Loops, and Encryption
Chapter 2 Bits, Data Types, and Operations
Data Representation COE 301 Computer Organization
Chapter 2 Bits, Data Types, and Operations
ASCII Character Codes nul soh stx etx eot 1 lf vt ff cr so
Introduction to Computer Engineering
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Cosc 2P12 Week 2.
Number Systems Lecture 2.
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
School of Computer Science and Technology
Data Representation ICS 233
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Characters Lesson Outline
Introduction to Computer Engineering
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Rayat Shikshan Sanstha’s S. M. Joshi College, Hadapsar
Text Representation ASCII Collating Sequence
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Cosc 2P12 Week 2.
Data Representation COE 308 Computer Architecture
Chapter 2 Bits, Data Types, and Operations
Presentation transcript:

CS 2130 Lecture 23 Data Types

Scalar Types int float char long short boolean enums*

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

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* 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

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 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

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 - 1 - B

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

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

N = 8 Binary 00000000 00000001 00000010 ... 01111111 10000000 10000001 11111110 11111111 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

N = 32 Binary 00000000000000000000000000000000 00000000000000000000000000000001 00000000000000000000000000000010 ... 01111111111111111111111111111111 10000000000000000000000000000000 10000000000000000000000000000001 11111111111111111111111111111110 11111111111111111111111111111111 Hex 1 2 ... 7FFFFFFF 80000000 80000001 FFFFFFFE FFFFFFFF Unsigned 1 2 ... 2147483647 2147483648 2147483649 4294967294 4294967295

N = 32 Hex 1 2 ... 7FFFFFFF 80000000 80000001 FFFFFFFE FFFFFFFF 1 2 ... 7FFFFFFF 80000000 80000001 FFFFFFFE FFFFFFFF Unsigned 1 2 ... 2147483647 2147483648 2147483649 4294967294 4294967295 Signed Mag 1 2 ... 2147483647 -0 -1 -2147483646 -2147483647 One's Comp 1 2 ... 2147483647 -2147483647 -2147483646 -1 -0 Two's Comp 1 2 ... 2147483647 -2147483648 -2147483647 -2 -1

Where do bits come from? Bitaba Tree Andes Mountains Numeric Bits 14,000' Numeric Bits 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... Why two codes? ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 000 000 000 00000000 NUL (Null char.) 001 001 001 00000001 SOH (Start of Header) 002 002 002 00000010 STX (Start of Text) 003 003 003 00000011 ETX (End of Text) 004 004 004 00000100 EOT (End of Transmission) 005 005 005 00000101 ENQ (Enquiry) 006 006 006 00000110 ACK (Acknowledgment) 007 007 007 00000111 BEL (Bell) 008 010 008 00001000 BS (Backspace) 009 011 009 00001001 HT (Horizontal Tab) 010 012 00A 00001010 LF (Line Feed) 011 013 00B 00001011 VT (Vertical Tab) 012 014 00C 00001100 FF (Form Feed) 013 015 00D 00001101 CR (Carriage Return) 014 016 00E 00001110 SO (Serial In)(Shift Out) 015 017 00F 00001111 SI (Serial Out)(Shift Out) 016 020 010 00010000 DLE (Data Link Escape) 017 021 011 00010001 DC1 (XON) (Device Control 1) 018 022 012 00010010 DC2 (Device Control 2) 019 023 013 00010011 DC3 (XOFF)(Device Control 3) 020 024 014 00010100 DC4 (Device Control 4) Why two codes?

Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 021 025 015 00010101 NAK (Negative Acknowledgement) 022 026 016 00010110 SYN (Synchronous Idle) 023 027 017 00010111 ETB (End of Trans. Block) 024 030 018 00011000 CAN (Cancel) 025 031 019 00011001 EM (End of Medium) 026 032 01A 00011010 SUB (Substitute) 027 033 01B 00011011 ESC (Escape) 028 034 01C 00011100 FS (File Separator) 029 035 01D 00011101 GS (Group Separator) 030 036 01E 00011110 RS (Request to Send)(Record Separator) 031 037 01F 00011111 US (Unit Separator) 032 040 020 00100000 SP (Space) 033 041 021 00100001 ! 034 042 022 00100010 " 035 043 023 00100011 # 036 044 024 00100100 $ 037 045 025 00100101 % 038 046 026 00100110 & 039 047 027 00100111 ' 040 050 028 00101000 (

Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 041 051 029 00101001 ) 042 052 02A 00101010 * 043 053 02B 00101011 + 044 054 02C 00101100 , 045 055 02D 00101101 - 046 056 02E 00101110 . 047 057 02F 00101111 / 048 060 030 00110000 0 049 061 031 00110001 1 050 062 032 00110010 2 051 063 033 00110011 3 052 064 034 00110100 4 053 065 035 00110101 5 054 066 036 00110110 6 055 067 037 00110111 7 056 070 038 00111000 8 057 071 039 00111001 9 058 072 03A 00111010 : 059 073 03B 00111011 ; 060 074 03C 00111100 <

Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 061 075 03D 00111101 = 062 076 03E 00111110 > 063 077 03F 00111111 ? 064 100 040 01000000 @ 065 101 041 01000001 A 066 102 042 01000010 B 067 103 043 01000011 C 068 104 044 01000100 D 069 105 045 01000101 E 070 106 046 01000110 F 071 107 047 01000111 G 072 110 048 01001000 H 073 111 049 01001001 I 074 112 04A 01001010 J 075 113 04B 01001011 K 076 114 04C 01001100 L 077 115 04D 01001101 M 078 116 04E 01001110 N 079 117 04F 01001111 O 080 120 050 01010000 P

Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 081 121 051 01010001 Q 082 122 052 01010010 R 083 123 053 01010011 S 084 124 054 01010100 T 085 125 055 01010101 U 086 126 056 01010110 V 087 127 057 01011111 W 088 130 058 01011000 X 089 131 059 01011001 Y 090 132 05A 01011010 Z 091 133 05B 01011011 [ 092 134 05C 01011100 \ 093 135 05D 01011101 ] 094 136 05E 01011110 ^ 095 137 05F 01011111 _ 096 140 060 01100000 ` 097 141 061 01100001 a 098 142 062 01100010 b 099 143 063 01100011 c 100 144 064 01100100 d

Don't forget... ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 101 145 065 01100101 e 102 146 066 01100110 f 103 147 067 01100111 g 104 150 068 01101000 h 105 151 069 01101001 i 106 152 06A 01101010 j 107 153 06B 01101011 k 108 154 06C 01101100 l 109 155 06D 01101101 m 110 156 06E 01101110 n 111 157 06F 01101111 o 112 160 070 01110000 p 113 161 071 01110001 q 114 162 072 01110010 r 115 163 073 01110011 s 116 164 074 01110100 t 117 165 075 01110101 u 118 166 076 01110110 v 119 167 077 01110111 w 120 170 078 01111000 x

Don't forget... Why is this at the end? ASCII Table (7-bit) Decimal Octal Hex Binary Value ------- ----- --- ------ ----- 121 171 079 01111001 y 122 172 07A 01111010 z 123 173 07B 01111011 { 124 174 07C 01111100 | 125 175 07D 01111101 } 126 176 07E 01111110 ~ 127 177 07F 01111111 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 Use some other calculator 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 odd 1 44 even 0 22 even 0 11 odd 1 5 odd 1 01011001

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

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

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

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 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 010110012 = 8910 110011012 = -7710 If signs are different sign of result will be sign of larger operand

Signed Magnitude? What does this represent? 010110012 = 8910 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 010110012 = 8910 110011012 = -5010

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

One's Complement? What does this represent? 010110012 = 8910 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 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 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 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 010110012 = 8910 110011012 = -5110 1001001102 3810 3810

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

Consider 1 digit decimal numbers 2 2 -2 2 -2 -2 2 4 4 -4 -4 -2 Positive numbers: 0 1 2 3 4 Negative numbers: 9 8 7 6 5 (-1 -2 -3 -4 -5) 2 2 -2 2 -2 -2 2 4 4 -4 -4 -2 4 6 2 -2 -6 -4 2 2 8 2 8 8 2 4 4 6 6 8 4 6 12 8 14 16 Bad Ok Ok Bad

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

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) 2 -2 2 -2 3 3 -3 -3 5 1 -1 -5 010 110 010 110 011 011 101 101 0101 1001 0111 1011 Bad Ok Ok 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: Write down 9's complement 57346 Then add 1

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: 00111000 Take 1's complement: 11000111 Add 1: 11001000

Binary works the same way! 2 -2 2 -2 3 3 -3 -3 5 1 -1 -5 010-0 110-1000 010- 0 110 -1000 011-0 011- 0 101-1000 101 -1000 101 1001-1000 111-1000 1011-10000 -3 1 -1 -5 Bad Ok Ok 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 0100 4 0111 7 (less 10...okay) 0111 7 1000 8 1111 15 (>9 add 6) 0110 1 0101 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?