1.Consider the following Java declarations and assignment statements. byte num1;
ID: 3768499 • Letter: 1
Question
1.Consider the following Java declarations and assignment statements.
byte num1; short num2;
num1 = <some unknown value>;
num2 = num1;
In the first assignment statement, some unknown value is assigned to the variable num1. It could be negative, zero, or positive. Assume that it results in the following eight bits in the variable num1:
b7 b6 b5 b4 b3 b2 b1 b0
Again, we do not know what these bits are because we do not know the value assigned. For convenience in discussion we are calling them b7-b0. What will be the resulting sixteen bits at the end of the second assignment statement in the variable num2, to ensure that num2 has the same value as num1? (The final answer will involve the eight bits b7-b0.)
Explanation / Answer
1) Since negative numbers have their MSB = 1 i.e. if num1 is a negative num then b7 = 1
2) (11111111 11101010)short = (11101010)byte for negative numbers
and (00000000 00001101)short = (00001101)byte for positive numbers
Using 2) we can say that the 16 bit representation of num2 will be
b7 b7 b7 b7 b7 b7 b7 b7 b7 b6 b5 b4 b3 b2 b1 b0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.