Beruflich Dokumente
Kultur Dokumente
Integer Specifications
The set of integer values defined for Integer
Data Type forms an ordered subset within
some finite bounds of the infinite set of
integers studied in mathematics.
The max integer value is sometimes
represented as a language-defined constant.
Typical values of maximum integer would be
32767, (215 1), on PC and minicomputers
2,147,483,647 , (231 1) , on large computers.
2
Integer Specifications
Using a Radix R, an integer is represented by
number =
i=0
di R i
and 0 di R 1
Example
(122) = 1 * 102 + 2 * 101 + 2*100
R=10
Arithmetic Operations
Binary Operations
Unary Operations
Other operations are included as library function
subprograms.
Relational Operations
Assignment Operations
4
addition . . . . . . . . . . . . . . . . . . . . . . . +
subtraction . . . . . . . . . . . . . . . . . . . . .
multiplication
.................
modulo . . . . . . . . . . . . . . . . . . . . . . . mod
identity . . . . . . . . . . . . . . . . . . . . . . . +
negation . . . . . . . . . . . . . . . . . . . . . .
Relational Operations
Relational operations have the specification :
equal . . . . . . . . . . . . . . . .
not equal . . . . . . . . . . . .
greater than . . . . . . . . . .
less than . . . . . . . . . . . .
greater than or equal . .
less than or equal . . . . .
>
<
Assignment Operations
An assignment between integer data objects is specified by:
void
it has no value, BUT it has a side effect of changing the contents of its
left operand.
Assignment operator usually indicates the direction of the assignment,
right-to-left, typically; = in Java.
integer
e.g.,
Q = 10 + ( P = 2 ) ( Q = 5 A B ) ;
8
Integers
Arithmetic will be performed by the computer
on integer values CORRECTLY if all the
operands and all the intermediate results lie in
the range of integer data.
If the allowable range is exceeded, the result of
the arithmetic is unpredictable. This is called
overflow.
Overflow Example
// This program demonstrates overflow error
in integer data
// value of the largest integer is 32767
class OverflowTest {
public static void main ( String[] args )
{
int a ;
char c ;
for (int i = 0 ; i <= 5 ; i++ )
{
a = i + 32765 ;
System.out.println(a);
}
}
}
output is :
32765
32766
32767
-32768 overflow occurs here
-32767
-32766
10
Representation of integers
External representation
Integer constants are expressed in programming languages
as a string of decimal digits optionally preceded by a sign.
The programming language may also define notation to
express a value in other number systems (binary, octal,
hexadecimal)
Internal representation
Binary Integers
Sign and Magnitude
Twos complements
Decimal
11
max magnitude is , 2 0 +2 1 +2 2 + .. +2 n2 = 2 n1 1
Range of integers that can be represented using n-bits:
(2 n1 1) integer + (2 n1 1)
Easy to inderstand
Disadvantage
12
14
Overflow Error
The ordinary arithmetic axioms can not be applied in
general to computer arithmetic. They do not hold in
cases where the true result of an operation lies outside
the given finite range of values, and an overflow error
occurs.
overflow
overflow
range of integers
0
Overflow Error
Integers
In Java
Boolean
char
byte
short
int
long
float
double