• Post Reply Bookmark Topic Watch Topic
  • New Topic

Type conversion 2  RSS feed

 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here too the results after initiation are:



double accurate = 12345.6 / 7.8;
int whole = (int) accurate;
byte overflow = (byte) whole;

accurate=1582.769230769231
whole= 1582
overflow=46

i do not understand how the value of overflow resoles to 46. PLease help.

Thank you
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you expect it to be? Think about what the size of a byte is.
 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A byte ranges from -127 to 127. and int is larger. But how was the number trimmed. Does one have to convert to bits first or so ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A byte is -128 to 127. So there are 256 values. If you keep subtracting 256 from 1582 until you're within the range of a byte you get 46 (1582 % 256 = 46).

You're on the right track regarding converting to bits. What's happening is this. The int is stored as a binary. When you convert to a byte you just lose all the higher bits that don't fit into a byte.

So in this case:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!