|
|
DEMO: /home/cs255001/demo/java/ShowIntConv.java
Background information on the CPU:
|
Consequently:
|
The computer can represent signed values using the 8 bits 2s complement code:
The computer can perform arithmetic (e.g.: +) operation on two 8 bits 2s complement codes
The computer can represent signed values using the 16 bits 2s complement code:
The computer can perform arithmetic (e.g.: +) operation on two 16 bits 2s complement codes
However:
The computer can not perform arithmetic (e.g.: +) operation on mixed 2s complement codes
Solution:
The computer first converts one 2s complement represention to the other 2s complement represention
The goal of conversion is:
|
List of values and their 2s compl representation in byte and int data type:
Value byte repr int repr ------- ---------- --------------------------------- 127 01111111 00000000000000000000000001111111 ... 3 00000011 00000000000000000000000000000011 2 00000010 00000000000000000000000000000010 1 00000001 00000000000000000000000000000001 0 00000000 00000000000000000000000000000000 -1 11111111 11111111111111111111111111111111 -2 11111110 11111111111111111111111111111110 -3 11111101 11111111111111111111111111111101 ... -128 10000000 11111111111111111111111110000000 |
Can you see how to convert an byte repr of some value into an int repr ?
Conversion: Extend the sign bit of the byte representation to fill 32 bits
The sign bit is the left-most bit in the byte representation
Value byte repr int repr ------- ---------- --------------------------------- 127 01111111 00000000000000000000000001111111 ... 3 00000011 00000000000000000000000000000011 2 00000010 00000000000000000000000000000010 1 00000001 00000000000000000000000000000001 0 00000000 00000000000000000000000000000000 -1 11111111 11111111111111111111111111111111 -2 11111110 11111111111111111111111111111110 -3 11111101 11111111111111111111111111111101 ... -128 10000000 11111111111111111111111110000000 |
We call this operation: sign extension
Notice that:
for the
value in the
range [−128, 127],
the last 8 bits in
the int repr
and byte repr
are equal:
Value byte repr int repr ------- ---------- --------------------------------- ... 128 Not possible 00000000000000000000000100000000 127 01111111 00000000000000000000000001111111 ... 3 00000011 00000000000000000000000000000011 2 00000010 00000000000000000000000000000010 1 00000001 00000000000000000000000000000001 0 00000000 00000000000000000000000000000000 -1 11111111 11111111111111111111111111111111 -2 11111110 11111111111111111111111111111110 -3 11111101 11111111111111111111111111111101 ... -128 10000000 11111111111111111111111110000000 -129 Not possible 11111111111111111111111101111111 ... |
Conversion: truncate the int repr to a byte by keeping the right-most 8 bits
However: this conversion is only safe when the value of the int repr is in the range [−128, 127]
Value byte repr int repr ------- ---------- --------------------------------- ... 128 Not possible 00000000000000000000000100000000 127 01111111 00000000000000000000000001111111 ... 3 00000011 00000000000000000000000000000011 2 00000010 00000000000000000000000000000010 1 00000001 00000000000000000000000000000001 0 00000000 00000000000000000000000000000000 -1 11111111 11111111111111111111111111111111 -2 11111110 11111111111111111111111111111110 -3 11111101 11111111111111111111111111111101 ... -128 10000000 11111111111111111111111110000000 -129 Not possible 11111111111111111111111101111111 ... |
One place where int to byte conversion (and vice versa) is performed is in assignment statements:
int i; byte b; i = b; // Converts byte repr in b tnto int repr // before storing the representation in i b = (byte) i; // Converts int repr in i tnto byte repr // before storing the representation in b |
Quiz:
|
Quiz:
|
Answer:
4
(because b contains
00000100)
Demo:
/home/cs255001/demo/java/JavaByteIntCast.java
List of values and their 2s compl representation in short and int data type:
Value short repr int repr ------- ----------------- --------------------------------- 32767 0111111111111111 00000000000000000111111111111111 ... 3 0000000000000011 00000000000000000000000000000011 2 0000000000000010 00000000000000000000000000000010 1 0000000000000001 00000000000000000000000000000001 0 0000000000000000 00000000000000000000000000000000 -1 1111111111111111 11111111111111111111111111111111 -2 1111111111111110 11111111111111111111111111111110 -3 1111111111111101 11111111111111111111111111111101 ... -32768 1000000000000000 11111111111111111000000000000000 |
Can you see how to convert an short repr of some value into an int repr ?
Conversion: Extend the sign bit of the short representation to fill 32 bits
The sign bit is the left-most bit in the short representation
Value short repr int repr ------- ----------------- --------------------------------- 32767 0111111111111111 00000000000000000111111111111111 ... 3 0000000000000011 00000000000000000000000000000011 2 0000000000000010 00000000000000000000000000000010 1 0000000000000001 00000000000000000000000000000001 0 0000000000000000 00000000000000000000000000000000 -1 1111111111111111 11111111111111111111111111111111 -2 1111111111111110 11111111111111111111111111111110 -3 1111111111111101 11111111111111111111111111111101 ... -32768 1000000000000000 11111111111111111000000000000000 |
This operation is: sign extension
Notice that:
for the
value in the
range [−32768, 32767],
the last 16 bits in
the int repr
and short repr
are equal:
Value short repr int repr ------- ----------------- --------------------------------- 32767 0111111111111111 00000000000000000111111111111111 ... 3 0000000000000011 00000000000000000000000000000011 2 0000000000000010 00000000000000000000000000000010 1 0000000000000001 00000000000000000000000000000001 0 0000000000000000 00000000000000000000000000000000 -1 1111111111111111 11111111111111111111111111111111 -2 1111111111111110 11111111111111111111111111111110 -3 1111111111111101 11111111111111111111111111111101 ... -32768 1000000000000000 11111111111111111000000000000000 |
Conversion: truncate the int repr down to 16 bits keeping the right-most bits
However: this conversion is only safe when the value of the int repr is in the range [−32768, 32767]
Value short repr int repr ------- ----------------- --------------------------------- 32767 0111111111111111 00000000000000000111111111111111 ... 3 0000000000000011 00000000000000000000000000000011 2 0000000000000010 00000000000000000000000000000010 1 0000000000000001 00000000000000000000000000000001 0 0000000000000000 00000000000000000000000000000000 -1 1111111111111111 11111111111111111111111111111111 -2 1111111111111110 11111111111111111111111111111110 -3 1111111111111101 11111111111111111111111111111101 ... -32768 1000000000000000 11111111111111111000000000000000 |
Quiz:
|
Quiz:
|
Answer:
6
(because s contains
0000000000000110)
Demo:
/home/cs255001/demo/java/JavaShortIntCast.java