简体   繁体   中英

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?

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 10
-1 11
0 00
1 01

So, that's what you get with two-bit long numbers. Note that the smallest value is -2 and the largest is 1.

Now, let's move on to three bits:

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

The smallest representable value is -4, and the largest one is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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