Range of signed char

前端 未结 6 978
盖世英雄少女心
盖世英雄少女心 2020-12-02 21:13

Why the range of signed character is -128 to 127 but not -127 to 128 ?

6条回答
  •  -上瘾入骨i
    2020-12-02 21:53

    If you just consider twos complement as arithmetic modulo 256, then the cutoff between positive and negative is purely arbitrary. You could just as well have put it at 63/-192, 254/-1, 130/-125, or anywhere else. However, as a standard signed integer format, twos complement came by convention put put the cutoff at 127/-128. This cutoff has one big benefit: the high bit being set corresponds directly to the number being negative.

    As for the C language, it leaves the format of signed numbers up to the implementation, but only offers 3 choices of implementation, all of which use a "sign bit": sign/magnitude, ones complement, and twos complement.

提交回复
热议问题