NDP Prasad

Ranch Hand

Posts: 177

posted 10 years ago

The below code is from Dan Chisholm

class Magenta {

static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;

public static void main(String args[]) {

System.out.print(a + " " + b + " " + c + " " + d);

}}

What is the result of attempting to compile and run the program?

a. Prints: 127 128 255 256

b. Prints: 127 128 255 0

c. Prints: 127 -1 -127 0

d. Prints: 127 -128 -1 0

e. Run-time error

f. Compile-time error

g. None of the above

I want to know the output of the program and Explanation of how it comes?

class Magenta {

static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;

public static void main(String args[]) {

System.out.print(a + " " + b + " " + c + " " + d);

}}

What is the result of attempting to compile and run the program?

a. Prints: 127 128 255 256

b. Prints: 127 128 255 0

c. Prints: 127 -1 -127 0

d. Prints: 127 -128 -1 0

e. Run-time error

f. Compile-time error

g. None of the above

I want to know the output of the program and Explanation of how it comes?

SCJP,SCWCD<br />

Priya Viswam

Ranch Hand

Posts: 81

posted 10 years ago

Output is d : Prints 127 -128 -1 0

since 127 can fit into a byte 'a' will print 127

128 in binary is 1000 0000. Here the first bit is the sign bit. First bit 1

indicates it is a negative number. Inorder to get the actual value we have

to take 2's compliment.

For getting the Twos compliment we have to find the 1's complement and add 1 to it.

1000 0000 -> 0111 1111 (1's compliment) -> 1000 0000(2's complement) -> 128

So b will print -128.

Similar way c will print -1.

255 in binary is -> 0 1111 1111. After casting to byte it will become 1111 1111. Here also the sign bit is 1 and so we have to take 2's complement.

1111 1111 -> 0000 0000 -> 0000 0001

So c will print -1

256 in binary is -> 1 0000 0000. After casting to byte it will become 0000 0000. Sign bit is zero. So no need to take the 2's complement and the value is zero.

since 127 can fit into a byte 'a' will print 127

128 in binary is 1000 0000. Here the first bit is the sign bit. First bit 1

indicates it is a negative number. Inorder to get the actual value we have

to take 2's compliment.

For getting the Twos compliment we have to find the 1's complement and add 1 to it.

1000 0000 -> 0111 1111 (1's compliment) -> 1000 0000(2's complement) -> 128

So b will print -128.

Similar way c will print -1.

255 in binary is -> 0 1111 1111. After casting to byte it will become 1111 1111. Here also the sign bit is 1 and so we have to take 2's complement.

1111 1111 -> 0000 0000 -> 0000 0001

So c will print -1

256 in binary is -> 1 0000 0000. After casting to byte it will become 0000 0000. Sign bit is zero. So no need to take the 2's complement and the value is zero.

SCJP 1.5<br />SCWCD 1.4

Anu Rastogi

Greenhorn

Posts: 3

posted 10 years ago

I am also in the process of preparing for JCP. So please correct me if my understanding is incorrect

A byte is represented by 8 bits of which the leftmost bit is the sign bit. This is 1 for negative numbers and 0 for positive numbers. The range for a byte is -128(1000 0000) to 127(0111 1111).

When byte a =(byte)127 is evaluated, 127 is in the range for a byte, so the output is 127.

For byte b=(byte)128, this is represented in binary as 1000 0000 which is equivalent to -128.

For byte c=(byte)255, this is represented as 1111 1111. This is a negative number. A negative of a number is found by taking 2s complement.(taking 1's complement and adding 1). So if we reverse this process on this number(subtracting 1 and taking 1s complement), we get -1.

For byte d=(byte)256, this is represented in binary as 1 0000 0000. Since this has 9 bits and a byte is represented with 8 bits, the 9th bit is discarded and we get 0 as the result.

A byte is represented by 8 bits of which the leftmost bit is the sign bit. This is 1 for negative numbers and 0 for positive numbers. The range for a byte is -128(1000 0000) to 127(0111 1111).

When byte a =(byte)127 is evaluated, 127 is in the range for a byte, so the output is 127.

For byte b=(byte)128, this is represented in binary as 1000 0000 which is equivalent to -128.

For byte c=(byte)255, this is represented as 1111 1111. This is a negative number. A negative of a number is found by taking 2s complement.(taking 1's complement and adding 1). So if we reverse this process on this number(subtracting 1 and taking 1s complement), we get -1.

For byte d=(byte)256, this is represented in binary as 1 0000 0000. Since this has 9 bits and a byte is represented with 8 bits, the 9th bit is discarded and we get 0 as the result.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |