Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# whizlabs question

kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Hi,

I found this question in whizlabs mocks.

byte b = (byte) 256;

System.out.println(b);

Value of b printed as _________.

"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

Joined: Dec 20, 2001
Posts: 3271
Kapil,

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

SCJP Tipline, etc.
Abhijeet Nagre
Greenhorn

Joined: Oct 05, 2004
Posts: 4
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.

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

subject: whizlabs question