Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Mock Exam Question

Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
I need help understanding this code.
I know that the highest number a byte can be is 127, but I don't see the connection:
(byte)128 = -128
(byte)255 = -1
(byte)256 = 0
Thank you for any responses
class E2 {
static byte a = (byte)127;
static byte b = (byte)128;
static byte c = (byte)255;
static byte d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}
}
/*
127 -128 -1 0
*/
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
This campfire story might be helpful.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Thank you for the response and posting great mock exams. I am learning a lot about Java from your site.
I have read that campfire story and I enjoyed it very much. I wish there was for 3D arrays.
I am assuming that the probelm that I posted above:
when the amount exceeds 2^7-1 and it automatically is forced to -1.
Thanks again
Stan Forest
Greenhorn

Joined: Mar 03, 2003
Posts: 12
A byte occupies eight bits
+127 is represented as: 0111 1111
The first digit is a sign, 0 is positive in this case. If the left first digit was a 1, the value would be negative.
128 in 32 bit binary is 0000 0000 0000 0000 0000 0000 1000 0000
when casting to a byte, the left 24 bits are discarded. The remaining is
1000 0000
This is now a byte. Because the first digit is a 1 the value is negative. Convert using two's complement. Twos complement requires inverting and adding 1.
start 1000 0000
invert 0111 1111
result 1000 0000 = 128 and it was negative = -128

256 is in binary is :
0000 0000 0000 0000 0000 0001 0000 0000
cast to a byte discards the left 24 bits leaving
0000 0000 which is 0
255 is one less than 256 so it is �1
OR
0000 0000 0000 0000 0000 0000 1111 1111
cast to a byte leaves
1111 1111
The first digit is 1 so it is negative so it needs to be converted using two's complement
start 1111 1111
invert 0000 0000
result 0000 0001 and remember it is negative so it is -1
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Thank you!
That helped!
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Jacob Michaels:
I wish there was for 3D arrays.

I'm not sure I understand this statement. Given Java's Collections and Maps, it's entirely possible to store an array of Map objects in another Map object and retrieve by key.

Regards,
Jim
SCJP, SCJD, SCWCD, SCEA Part I
Rattan Mann
Ranch Hand

Joined: Jan 21, 2003
Posts: 44
On Dan's recommendation I too read the campfire stories. They are beautiful stories.But who is writing those beautiful,imaginative stories? I didn't find the name of the authors or am I so stupid that I missed the names?
Rattan
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The credits for the campfire story are provided at the bottom of the page.

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

subject: Mock Exam Question