This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting int to byte ?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice job Henry, I didn't read the question very well.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer i got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic