Search code examples
c++memoryuint8t

What happens to a value assigned to take up more than a types available space?


e.g.

uint8_t value = 256;

debug output:

0

I've read that it does some sort of truncating? I'm not seeing exactly how, any links are appreciated.


Solution

  • I'll try to make sense of it along with you.

    uint8_t is an 8-bit data type, or a byte. It has 8 slots which can either be 1 or 0. 1111 1111 would be 255. So if you ad one to it, it keeps carrying over. 255 + 1 in binary would be 1 0000 0000, but since the data type can only store 8 bits, it drops the 1, and becomes 0000 0000, which translates to the integer value 0.

    At least, that's how I understand it works.