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

# Conversion

Ranch Hand
Posts: 177
• Number of slices to send:
Optional 'thank-you' note:
The below code is from Dan Chisholm

class Magenta {
static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}

What is the result of attempting to compile and run the program?

a. Prints: 127 128 255 256
b. Prints: 127 128 255 0
c. Prints: 127 -1 -127 0
d. Prints: 127 -128 -1 0
e. Run-time error
f. Compile-time error
g. None of the above

I want to know the output of the program and Explanation of how it comes?

Ranch Hand
Posts: 81
• Number of slices to send:
Optional 'thank-you' note:
Output is d : Prints 127 -128 -1 0

since 127 can fit into a byte 'a' will print 127

128 in binary is 1000 0000. Here the first bit is the sign bit. First bit 1
indicates it is a negative number. Inorder to get the actual value we have
to take 2's compliment.
For getting the Twos compliment we have to find the 1's complement and add 1 to it.

1000 0000 -> 0111 1111 (1's compliment) -> 1000 0000(2's complement) -> 128
So b will print -128.

Similar way c will print -1.
255 in binary is -> 0 1111 1111. After casting to byte it will become 1111 1111. Here also the sign bit is 1 and so we have to take 2's complement.
1111 1111 -> 0000 0000 -> 0000 0001
So c will print -1

256 in binary is -> 1 0000 0000. After casting to byte it will become 0000 0000. Sign bit is zero. So no need to take the 2's complement and the value is zero.

Ranch Hand
Posts: 177
• Number of slices to send:
Optional 'thank-you' note:
Thank you Priya. I got it.

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
I am also in the process of preparing for JCP. So please correct me if my understanding is incorrect

A byte is represented by 8 bits of which the leftmost bit is the sign bit. This is 1 for negative numbers and 0 for positive numbers. The range for a byte is -128(1000 0000) to 127(0111 1111).

When byte a =(byte)127 is evaluated, 127 is in the range for a byte, so the output is 127.

For byte b=(byte)128, this is represented in binary as 1000 0000 which is equivalent to -128.

For byte c=(byte)255, this is represented as 1111 1111. This is a negative number. A negative of a number is found by taking 2s complement.(taking 1's complement and adding 1). So if we reverse this process on this number(subtracting 1 and taking 1s complement), we get -1.

For byte d=(byte)256, this is represented in binary as 1 0000 0000. Since this has 9 bits and a byte is represented with 8 bits, the 9th bit is discarded and we get 0 as the result.

Ranch Hand
Posts: 252
• Number of slices to send:
Optional 'thank-you' note:
We can also find the result with arithmetic division rather than dealing with bits.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?