270 likes | 390 Views
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:
E N D
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: integers, reals, characters, booleans, enumerations, pointers (references in SML)
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
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: =, <, >, <=, >=, =/=
S Data Binary integer Sign bit (0 for +, 1 for -) Integers - Implementation • Implementation: • Binary representation in 2’s complement arithmetic • Three different standard representations:
S Data Integers - Implementation • First kind: Binary integer Sign bit (0 for +, 1 for -)
T Address S Data T S Data Integers – Implementation • Secondkind • Third kind Type descriptor Sign bit Type descriptor Sign bit
0 1 0 0 1 1 0 0 Integer Numeric Data • Positive values 64 + 8 + 4 = 76 sign bit
1 1 1 1 1 1 1 1 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 0 1 1 0 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 2’s Complement Arithmetic • Initial value: = 76 • One’s Complement: = - 77 • Two’s Complement: = - 76
Subranges • Example (Ada): A:integer range 10..20 • Subtype of integers (implicit coercion into integer)
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
S E M 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)
Floating Point Numbers: Theory • Every non-zero number may be uniquely written as (-1)S * 2 e* m where 1 m < 2 and S is either 0 or 1
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
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.
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.
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
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
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
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
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…
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
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
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