Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Mock Exam Question

Jacob Michaels
Ranch Hand
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
Posts: 1865
This campfire story might be helpful.

Jacob Michaels
Ranch Hand
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
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
Posts: 35
Thank you!
That helped!

Jim Bedenbaugh
Ranch Hand
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.

Rattan Mann
Ranch Hand
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
Posts: 1865
The credits for the campfire story are provided at the bottom of the page.

 It is sorta covered in the JavaRanch Style Guide.