How do unsigned integers work

前端 未结 3 900
情书的邮戳
情书的邮戳 2021-01-13 21:13

As the title suggests, I am curious about how an unsigned int (or things like NSUInteger, u_int_blah, but I assume these are all

3条回答
  •  一向
    一向 (楼主)
    2021-01-13 22:02

    In answer to your first code example, the loop will compile (though will raise a warning if you are compiling with gcc and have the -Wno-sign-compare flag). Running it, however, will generally result in the loop not running at all since most systems use Two's complement (http://en.wikipedia.org/wiki/Two%27s_complement) which means -10 is the same as 4294967286 (assuming 4 byte integers but in general 2*INT_MAX - 10) which is greater than 5. In general, I recommend reading up on Two's complement as it will probably answer all your questions on the matter.

提交回复
热议问题