Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Exam Question

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
*/
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This campfire story might be helpful.
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
add 1
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
add 1
result 0000 0001 and remember it is negative so it is -1
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
That helped!
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The credits for the campfire story are provided at the bottom of the page.
 
please buy this thing and then I get a fat cut of the action:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic