programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Type conversion 2

Atah Tabotnjap
Greenhorn
Posts: 24
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

Thank you

Matthew Brown
Bartender
Posts: 4568
9
What did you expect it to be? Think about what the size of a byte is.

Atah Tabotnjap
Greenhorn
Posts: 24
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
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:

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