Search code examples
cmodulounsigned-integer

how does operation on unsigned integers actually take place?


i don't understand the modulo operation in the following paragraph:---

when overflow occurs during an operation on unsigned integers,though,the result is defined :we get the correct answer modulo 2^n , where n is the number of bits used to store the result.for example, if we add 1 to the unsigned 16-bit number 65,535 , the result is guaranteed to be 0.


Solution

  • Imagine a clock that has values with a range [0..11] (12 distinct values), when you say the time is 14:00 you can also say it is 2pm, which is 14 mod 12. The same happens when an integer overflows (65,536 mod 65,536 is 0). Whether the answer is semantically correct in your application depends on the application.