关于字符范围的困惑 [英] confusion regarding range of char

查看:68
本文介绍了关于字符范围的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道char的范围是 -128 127 .

As we know that the range of char is -128 to 127.

-128 的2的补码与 128 的二进制相同,即

The 2's compliment of -128 and the binary of 128 is same, which is

10000000

10000000

那么为什么char的范围是 -128 127 ,而不是 -127 128 .br>与 int 一样, 128 -128 都不同.

So why the range of char is -128 to 127 but not -127 to 128 .
Where as in case of int , 128 and -128 both are different.

推荐答案

以二进制补码表示,只要高阶位为1,数字均为负数.因此,最大的正数是

In twos-complement notation, whenever the high-order bit is 1, the number is negative. So the biggest positive number is

01111111 = 127

,最小的负数是

10000000 = -128

int 也会发生同样的事情,但是其范围要大得多.最大的正整数是

The same thing happens for int, but its range is much larger. The biggest positive int is

01111111 11111111 11111111 11111111 = 2147483647

并且最小的负整数是

10000000 00000000 00000000 00000000 = -2147483648

如您所见,在两种情况下,最小的负数都要比最大的正数多1.

As you can see, in both cases, the smallest negative is 1 more than the biggest positive.

您可能想知道,如果符号位以外的位数相同,为什么负数要比正数多?是因为您没有计算 0 .它的高阶位为0,因此在此表示法中被视为正数.包括在内时,有128个负数字符和128个非负数字符,并且它们平衡相等.

You might be wondering, If there are the same number of bits other than the sign bit, why are there more negative numbers than positives? That's because you're not counting 0. It has 0 in the high-order bit, so it's considered positive in this notation. When you include that, there are 128 negative chars, and 128 non-negative chars, and they balance equally.

这篇关于关于字符范围的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆