my dog learned polymorphism*
The moose likes Beginning Java and the fly likes  converting int to byte ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " converting int to byte ?" Watch " converting int to byte ?" New topic
Author

converting int to byte ?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all
i have a code like this
int a=8666;
byte b=(byte)a;
i am getting -38 as output. But i can say only the output after it gets print.How can i determine the value before looking to the output console.how can i solve this one mentally


thanks


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

how can i solve this one mentally


Well, I highly doubt that you can do this one mentally, but who knows... It can be done in three steps.

1. Convert the decimal number to binary. Since this number is positive, you can just do this somewhat easily.

2. Truncate the three high order bytes. This is what the Java compiler does.

3. Convert the binary back to decimal. Unfortunately, since the sign bit is on, you will need to do this via twos-complement. (google twos complement for more information)

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

For example...

8666 = 0000 0000 0000 0000 0010 0001 1101 1010 (to binary)

= 1101 1010 (truncated)

= 0010 0101 (negated)
= 0010 0110 (negated plus one)
= 38 (decimal after negated plus one)
= -38 (hence, decimal original)

Henry
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
It really depends on what the user wants the results to be when truncating the HO 3 bytes. 38 doesn't make any sense to me. Does he want to loose the value of the HO bit by treating that bit as the sign?
The value of the LO byte should be 218.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

Originally posted by Norm Radder:
It really depends on what the user wants the results to be when truncating the HO 3 bytes. 38 doesn't make any sense to me. Does he want to loose the value of the HO bit by treating that bit as the sign?
The value of the LO byte should be 218.


Maybe. But that wasn't the question. The question was what happens when an int is casted to a byte. And in that case, the higher order 3 bytes are truncated. And the remaining byte is treated as a signed byte -- which in the example, is valued at -38.

Henry
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Nice job Henry, I didn't read the question very well.
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks for the answer i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting int to byte ?
 
Similar Threads
Sign extension doubt?
How to access byte array web method
pls explain?
doubt in implict casting
Doubt in char array and byte array