programming language concepts (cis 635)

27
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www. cs . njit . edu /~elsa/635

Upload: rose-stokes

Post on 15-Mar-2016

35 views

Category:

Documents


2 download

DESCRIPTION

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Elementary Data Types. Data objects contain single data value with no components Standard elementary types include: - PowerPoint PPT Presentation

TRANSCRIPT

Page 2: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Elementary Data Types

• Data objects contain single data value with no components

• Standard elementary types include: integers, reals, characters,

booleans, enumerations, pointers (references in SML)

Page 3: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Specification of Elementary Data Types

• Basic attributes of type usually used by compiler and then discarded

• Some partial type information may occur in data object

• Values usually match with hardware types: 8 bits, 16 bits, 32 bits, 64 bits

• Operations: primitive operations with hardware support, and user-defined operations built from primitive ones

Page 4: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Integers – Specification

• Range of integers for some fixed minint to some fixed maxint, typically -2^31 through 2^31 – 1 or –2^30 through 2^30 - 1

• Standard collection of operators: +, -, *, /, mod, ~ (negation)

• Standard relational operations: =, <, >, <=, >=, =/=

Page 5: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Integers - Implementation

• Implementation:– Binary representation in 2’s

complement arithmetic– Three different standard

representations:S Data

Sign bit (0 for +, 1 for -) Binary integer

Page 6: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Integers - Implementation

• First kind:

S Data

Sign bit (0 for +, 1 for -) Binary integer

Page 7: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

• Second kind

• Third kind

T Address

Integers – Implementation

S Data

T S Data

Type descriptor

Type descriptor

Sign bit

Sign bit

Page 8: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Integer Numeric Data

• Positive values

64 + 8 + 4 = 76

0 1 0 0 1 1 0 0

sign bit

Page 9: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Integer Arithmetic – Theory

• Based on idea that given n bits, use 2n = 0 mod 2n

• 2n – 1 = 2n-1 + . . . + 2 + 1 = - 1 mod 2n

• - 1 1 1 1 1 1 1 1 1

Page 10: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

2’s Complement Arithmetic• Initial value: = 76• One’s Complement: = - 77• Two’s Complement: = - 76

1 0 1 1 0 0 1 1

1 0 1 1 0 1 0 0

0 1 0 0 1 1 0 0

Page 11: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Page 12: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Subranges

• Example (Ada): A:integer range 10..20

• Subtype of integers (implicit coercion into integer)

Page 13: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Subranges

• Data may require fewer bits than integer type–Data in example above require

only 4 bits• Range checking usually requires

some runtime time information and dynamic type checking

Page 14: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

IEEE Floating Point Format

• IEEE standard 754 specifies both a 32- and 64-bit standard

• At least one supported by most hardware

• Numbers consist of three fields:– S (sign), E (exponent), M (mantissa)

S E M

Page 15: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Floating Point Numbers: Theory

• Every non-zero number may be uniquely written as

(-1)S * 2 e * mwhere 1 m < 2 and S is either 0 or 1

Page 16: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Floating Point Numbers: Theory

• Every non-zero number may be uniquely written as

(-1)S * 2 (E – bias) * (1 + (M/2N))where 0 M < 1

• N is number of bits for M (23 or 52)• Bias is 127 of 32-bit ints, 1023 for

64-bit

Page 17: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

IEEE Floating Point Format (32 Bits)

• S: a one-bit sign field. 0 is positive.• E: an exponent in excess-127

notation. Values (8 bits) range from 0 to 255, corresponding to exponents of 2 that range from -127 to 128.

Page 18: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

IEEE Floating Point Format (32 Bits)

• M: a mantissa of 23 bits. Since the first bit of the mantissa in a normalized number is always 1, it can be omitted and inserted automatically by the hardware, yielding an extra 24th bit of precision.

Page 19: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Exponent Bias

• If 8 bits (256 values) +127 added to exponent to get E

• If E = 127 then 127-127 = 0 is true exponent

• If E = 129 then 129-127 = 2 is true exponent

• If E = 120 then 120-127 = -7 is true exponent

Page 20: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Floating Point Number Range

• In 32-bit format, the exponent has 8 bits giving a range from –127 to 128 for exponent

• This give a number range from 10-38 to 1038 roughly speaking

Page 21: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Floating Point Number Range

• In 64-bit format,the exponent is extended to 11 bits giving a range from -1023 to +1024 for the exponent

• This gives a range from 10-308 to 10308 roughly speaking

Page 22: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Decoding IEEE format

• Given E, and M, the value of the representation is:Parameters Value

• E=255 and M 0 An invalid number• E=255 and M = 0 • 0<E<255 2{E-127}(1+(M/ 223))• E=0 and M 0 2 -126 (M / 223)• E=0 and M=0 0

Page 23: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Example Floating Point Numbers

• +1= 20*1= 2{127-127}*(1 + .0) 0 01111111 000000…• +1.5= 20*1.5= 2{127-127}*(1+ 222/ 223) 0 01111111 100000…• -5= -22*1.25= 2{129-127}*(1+ 221/ 223) 1 10000001 010000…

Page 24: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Page 25: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Other Numeric Data

• Short integers (C) - 16 bit, 8 bit• Long integers (C) - 64 bit• Boolean or logical - 1 bit with value

true or false (often stored as bytes)• Byte - 8 bits

Page 26: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Other Numeric Data

• Character - Single 8-bit byte - 256 characters

• ASCII is a 7 bit 128 character code• Unicode is a 16-bit character code

(Java)• In C, a char variable is simply 8-bit

integer numeric data

Page 27: Programming Language Concepts (CIS 635)

Copyright 2002 Elsa L. Gunter

Enumerations• Motivation: Type for case analysis over

a small number of symbolic values• Example: (Ada)

Type DAYS is {Mon, Tues, Wed, Thu, Fri, Sat, Sun}

• Implementation: Mon 0; … Sun 6• Treated as ordered type (Mon < Wed)• In C, always implicitly coerced to

integers