A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Joined: May 11, 2004
Oct 07, 2004 09:15:00
I found this question in whizlabs mocks.
byte b = (byte) 256;
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.
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.
SCJP 1.4, SCWCD 1.4
Joined: Dec 20, 2001
Oct 07, 2004 09:24:00
Your method is just fine. In addition, you can check out
this SCJP Tipline article
for more details.
SCJP Tipline, etc.
Joined: Oct 05, 2004
Oct 07, 2004 12:29:00
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
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:
subject: whizlabs question
Implicit and explicit casts give different results?
how is this possible??--Help Wanted
Couple of questions on Operators
complement in byte if range exceeds
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014