- Suppose
the program needs an
integer value
for input in some
computation
- Since the integer value
is used for
computation, we want to use
the 2's complement representation
for the integer value
- However, the
key board device uses the
ASCII representation for
the input data
That means:
when the user types in:
The computer will
receive these
ASCII codes as
input:
But in order to perform
computation, the
number 12 must be
represented by
00000000000000000000000000001100 !!
- Therefore:
- We need to convert the
ASCII input representation
(emitted by the keyboard) to
the corresponing
2's complement representation
|
BTW: that's is why you need to use the following construct to read in
numbers in
a Java program:
String s = in.next( ); // Read input - it's always in ASCII code
int i = Integer.parseInt( s ); // Convert ASCII repr to 2's compl repr
|
(The method in.nextInt( )
combines the
in.next( ) and
the Integer.parseInt( ) for
your convenience.)
|
|