I was wondering if someone can explain to me what happens when we add binary numbers.
Say we have 4 bits, 0b1111 and we add 1 to 0b1111. I think the binary encoding should be 0b10000; however, since there are only 4 bits the bits will change to 0b0000.
I was wondering why this happens, is it because there's not enough space? How would we add 1 to it if 0b1111 was a signed integer?
if 0b1111
is an unsigned 4 bit value, with 4 bit storage, it has a value of 15. Adding 1 to it, will give you 16, which cannot be stored in 4 bits. The bits roll over and 0b0000
is stored, giving you a result of 0.
Now, if 0b1111
is a signed 4 bit value, with 4 bit storage. it is generally stored in the two's complement representation. It has a range of -8 to +7. 0b1111
will give you -1. (See here on how to convert.) Adding one to that gives you 0.