# whizlabs question

kapil munjal

Ranch Hand

Posts: 298

posted 11 years ago

- 0

Hi,

I found this question in whizlabs mocks.

byte b = (byte) 256;

System.out.println(b);

Value of b printed as _________.

Explanation of the answer is

"Since the integer's value (256) is larger than the byte's range (-128 to +127), the result is the remainder of the division of the integer value by the byte's range (256) which is zero"

I have not been able to understand the explanation of this answer.

I was thinking the solution in this way.....

00000000 00000000 00000001 00000000 (256 in Decimal)

when we cast it to byte then we are left with only the first 8 bits.

00000000

so after casting and after removing the other 24 bits, it will be 0.

I hope my method of doing it is right.

But I have not been able to understand the explanation of WHIZLABS mock.

Please someone explain that way of finding the value.

Thanks

Kaps

I found this question in whizlabs mocks.

byte b = (byte) 256;

System.out.println(b);

Value of b printed as _________.

Explanation of the answer is

"Since the integer's value (256) is larger than the byte's range (-128 to +127), the result is the remainder of the division of the integer value by the byte's range (256) which is zero"

I have not been able to understand the explanation of this answer.

I was thinking the solution in this way.....

00000000 00000000 00000001 00000000 (256 in Decimal)

when we cast it to byte then we are left with only the first 8 bits.

00000000

so after casting and after removing the other 24 bits, it will be 0.

I hope my method of doing it is right.

But I have not been able to understand the explanation of WHIZLABS mock.

Please someone explain that way of finding the value.

Thanks

Kaps

Kapil Munjal

SCJP 1.4, SCWCD 1.4

Corey McGlone

Ranch Hand

Posts: 3271

posted 11 years ago

- 0

Kapil,

Your method is just fine. In addition, you can check out this SCJP Tipline article for more details.

Your method is just fine. In addition, you can check out this SCJP Tipline article for more details.

Abhijeet Nagre

Greenhorn

Posts: 4

posted 11 years ago

- 0

Hi,

See 14 in binary is 1110

if you take only 1 LSB Bit the Ans is : 0

if you divide 14 BY 2(2 raise to 1) Ans is : 0

Simillerly,

if you take only 3 LSB Bits the Ans is : 6

and if you divide 14 BY 8(2 raise to 3) Ans is : 6

So when you take LSB Bits you indirectly divide the original no

By no which is 2 raise to the no of Bits you are removing.

See 14 in binary is 1110

if you take only 1 LSB Bit the Ans is : 0

if you divide 14 BY 2(2 raise to 1) Ans is : 0

Simillerly,

if you take only 3 LSB Bits the Ans is : 6

and if you divide 14 BY 8(2 raise to 3) Ans is : 6

So when you take LSB Bits you indirectly divide the original no

By no which is 2 raise to the no of Bits you are removing.

I agree. Here's the link: http://aspose.com/file-tools |