+ 1

Basic: Short and byte

I have a simple test that contains: short x = 150; byte y = (byte) (x+x); Why y = 44? I know that: x + x = 300 but now type int; byte y max value is 255, so answer should be it. What I am missing? Thank you in advance ☀️

19th Mar 2022, 3:58 PM
Aydar Sunagatov
Aydar Sunagatov - avatar
2 ответов
+ 2
300 - 256 = 44 byte can hold 8bit data, max = 255 All bits set as 1111 1111 adding 1 means 1 0000 0000 , => 256 adding 1 means 1 0000 0001 => 257 See byte can hold only 8bit data and last bit 1 is exceeds in storage , and ignored.. so in value, meaning it get repeating from zero again. So from 300 , byte now hold last 8bits and value is equal to 44. Hope it helps....
19th Mar 2022, 4:08 PM
Jayakrishna 🇮🇳
+ 2
I got it! It goes to 255 and goes to 0 again. That's why! Thank you a lot!
19th Mar 2022, 4:56 PM
Aydar Sunagatov
Aydar Sunagatov - avatar