Signed int range confusion

后端 未结 5 893
终归单人心
终归单人心 2021-01-29 09:10

This question might be very basic but i post here only after days of googling and for my proper basic understanding of signed integers in C.

Actually some say signed int

5条回答
  •  青春惊慌失措
    2021-01-29 09:49

    Today, signed ints are usually done in two's complement notation.

    The highest bit is the "sign bit", it is set for all negative numbers.

    This means you have seven bits to represent different values.

    With the highest bit unset, you can (with 16 bits total) represent the values 0..32767.

    With the highest bit set, and because you already have a representation for zero, you can represent the values -1..-32768.

    This is, however, implementation-defined, other representations do exist as well. The actual range limits for signed integers on your platform / for your compiler are the ones found in your environment's . That is the only definite authority.

    On today's desktop systems, an int is usually 32 or 64 bits wide, for a correspondingly much larger range than the 16-bit 32767 / 32768 you are talking of. So either those people are talking about really old platforms, really old knowledge, embedded systems, or the minimum guaranteed range -- the standard states that INT_MIN must be at least -32767, and INT_MAX be at least +32767, the lowest common denominator.

提交回复
热议问题