Sie sind auf Seite 1von 19

INTEGERS

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

Operations on Integer data-objects

Arithmetic Operations

Binary Operations
Unary Operations
Other operations are included as library function
subprograms.

Relational Operations
Assignment Operations
4

Binary Arithmetic Operations


Binary arithmetic operations have the specification :

binary operation: integer integer integer


where binary operation may be :

addition . . . . . . . . . . . . . . . . . . . . . . . +
subtraction . . . . . . . . . . . . . . . . . . . . .
multiplication
.................
modulo . . . . . . . . . . . . . . . . . . . . . . . mod

Unary Arithmetic Operations


Unary arithmetic operations have the specification :

Unary operation: integer integer


where unary operation may be :

identity . . . . . . . . . . . . . . . . . . . . . . . +
negation . . . . . . . . . . . . . . . . . . . . . .

post and pre increment in Java, C ++ . . .+ +


post and pre decrement in Java, C ++ . . . .

Relational Operations
Relational operations have the specification :

relational operation: integer integer Boolean


Where relational operation may be :

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:

assignment operation: integer integer

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.

In C, an assignment between integer data objects is specified:

assignment operation: integer integer

integer

i.e., it HAS a value. Assignment is an expression that can be used in:


chained or compound assignment :
e.g.,
X= Y= Z =0 ;
embedded assignment : used as operand in more complex expression .

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

Binary Coded Decimal (BCD). Also called Packed

Decimal
11

Binary Integers: Sign and Magnitude


Sign and magnitude:
sign
1bit

(one bit for sign , n 1 bits for magnitude )


magnitude
n 1 bits

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)

For n =16, 32767 integer +32767


Advantage

Easy to inderstand

Disadvantage

There are two zeros: one +ve and one ve


Causes difficulty in addition/subtraction because we have to know first the
operand sign

12

Binary Integers: Twos Complement


It is based on modular arithmetic which states that adding or subtracting the
modulus M from a number, its value does not change. If we have n bits
M = 2n
= 100.0
n zeros
This value is 1 unit larger than the largest# represented by n bits which is
2n-1
M = (2n-1) +1
2n-1 = M 1
Example n=4, M=16, largest# represented by 4 bits = 15
2s complement technique is based on the following principle
+ve # are represented by counting up from 0 to the value of the number
-ve # are represented by counting down the modulus M
Ex +5  counting 5 units up from 0

-4  counting 4 units down from Modulus M


This can be defined as +x = x and x = M-x
13

Binary Integers: Twos Complement


+x = x and x = M-x
Ex M=16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1
+ 6 = 0110
-7 = M-7 = 24 7 = 9 = 1001
Range of integers that can be represented using n-bits:
2 n1 integer + (2 n1 1)
Advantage
Eliminates the problem of ve zero
is simpler to implement arithmetic operations

14

BCD: Binary Coded Decimal (Packed


Decimal)
dn d1 d0 Sign

Four bits are used for each decimal digit


four bits for representation of the sign (usually,
1100 for positive value, 1101 for negative).
The sign of the number is written on rightmost end.
15

BCD: Binary Coded Decimal (Packed


Decimal)
dn d1 d0 Sign
This representation is suitable for commercial applications, where large
volume of data is to be input or output, but trivial processing is needed.
The reason is that the conversion between external representation of a value
(sequence of decimal digit characters), and its BCD representation is easier
than in case of binary representation, and can be done using single
machine instruction ( PACK and UNPACK instructions on IBMmachines) ,
This representation is less efficient w.r.t. storage requirement
an increase of 4-bits in the size allocated for an integer data object
results in
10-fold range when using BCD (one extra decimal digit)
16-fold range when using binary representation ( 24= 16 )
16

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

Aside from the phenomenon of overflow, all


operations on arguments of type integer are assumed
to be EXACT.
17

Overflow Error

order of operations may be important :

A + B C may give overflow, while


A C + B causes no overflow.

Negating value of a variable x= x, may give rise


to overflow if twos complement notation is used
to represent integers and the value of x is 2n1
In C, C++, Java , as well as other languages , there
are many types for integer data, each differs only
in the range of the allowed values : e.g., int ,
short int , long int , unsigned int , . . .
18

Integers
In Java
Boolean
char
byte
short
int
long
float
double

Boolean value: true or false


16-bit Unicode character
8-bit signed two's complement integer
16-bit signed two's complement integer
32-bit signed two's complement integer
64-bit signed two's complement integer
32-bit floating-point number (IEEE 754-1985)
64-bit floating-point number (IEEE 754-1985)

Integers are used for exact computations, as control


variables for iteration, as indices, and so on.
Unsigned integers are often used for counters and for
address arithmetic.
19

Das könnte Ihnen auch gefallen