简体   繁体   English

cpp中数据类型的范围

[英]Range of data type in cpp

Some where I have studied that in a signed data type one bit is used for sign so in that case positive value is 127 in char type as one can write + 1111111 in binary form so in negative also it should take value -127 as -1111111 but it is -128 why?在我研究过的一些地方,在有符号数据类型中,一位用于符号,所以在这种情况下,char 类型的正值是 127,因为可以以二进制形式写入 + 1111111,所以在负值时,它也应该取值 -127 作为 -1111111但它是-128 为什么?

Let's create a simple table, a list of all numbers that can be expressed in two, instead eight bits.让我们创建一个简单的表格,一个可以用两位而不是八位表示的所有数字的列表。 One bit is still used for the sign, and one bit for the actual value.一位仍用于符号,一位用于实际值。 If we do that, then this is the table that we'll end up with:如果我们这样做,那么这就是我们最终得到的表格:

decimal十进制 binary二进制
-2 -2 10 10
-1 -1 11 11
0 0 00 00
1 1 01 01

So, that's what you get with two-bit long numbers.所以,这就是你得到的两位长数字。 Note that the smallest value is -2 and the largest is 1.请注意,最小值为 -2,最大值为 1。

Now, let's move on to three bits:现在,让我们继续讨论三个位:

decimal十进制 binary二进制
-4 -4 100 100
-3 -3 101 101
-2 -2 110 110
-1 -1 111 111
0 0 000 000
1 1 001 001
2 2 010 010
3 3 011 011

The smallest representable value is -4, and the largest one is 3.最小的可表示值是-4,最大的是3。

If you continue on with this process, when you arrive at 8-bit numbers you will find that the smallest value will be -128 and largest one will be 127.如果继续这个过程,当你得到 8 位数字时,你会发现最小值是 -128,最大值是 127。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM