Analysis of why the range of char types is -128~+127

Source: Internet
Author: User
Tags truncated

in C, the range of signed char types is -128~127, which is written in every textbook, but no book (including the teacher) will give you a -128~127, and the question seems simple and easy enough not to think about why, Is there a formula for an integer range: -2^ (n-1) ~2^ (n-1)-1 n is the memory footprint of an integer, so the int type 32 bits is then-(2^31) ~2^31-1

-2147483648~2147483647, but why the minimum negative absolute value is always 1 more than the maximum positive number, this problem even some programmers who work for a few years are ambiguous, because did not think deeply, only know the book so write. So, I have to think deeply about this problem that many people neglect.

For unsigned integers, it is simple, all parts represent values, such as char type, 8 bits, with binary representation of 0000 0000 ~ 1111 1111

1111 1111 The maximum is the decimal 255, so the range of unsigned char is 0~ 255, here to popularize the method of 2 decimal, binary each bit of the value multiplied by its bit (2^ (n-1), n is the right-to-left bit), and then add, you can get the decimal number, such as:

1111 1111 =1*2^7+1*2^6+1*2^5+1*2^4+1*2^3+1*2^2+1*2^1+1*2^0=127.

However, for signed integers, the highest bit of binary is positive or negative, does not represent a numeric value, the highest bit is 0 to indicate a positive number, 1 is a negative number, so that the value of the remaining (n-1) bit, such as Char a=-1; Then the binary representation is 1 0000001, 1 is 0 0000001, so the signed char drop symbol bit the remaining 7 bits max 1111 111 = 127, and then add the symbol, 0 1111111=127, 1 1111111 = 127, the range should be For -127~127, the same type of int is the same, but the problem comes out, the textbook is -128~127 Ah, the following analysis of this amazing wonderful ...

And then popularize the computer internal integer storage, we all know that the internal computer is binary to store numerical values, unsigned integers will be used all for storage, signed integers, the highest bit as the symbol bit, the rest of the value, so plausible, but bring a trouble, when the addition,

0000 0001

+ 0000 0001

—————————

0000 0010 ............ 2

When subtracting, 1-1=? Since the computer will only add and not subtract, it will be converted to 1+ (-1), so

0000 0001

+ 1000 0001

____________________

1000 0010 ...-2, 1-1 = 2?..... This is obviously wrong, therefore, in order to avoid the subtraction operation error, the computer great God invented the anti-code, directly with the highest level of the symbol bit is called the original code, the above mentioned binary is the original code form, the inverse code is the original code in addition to the highest bit of the rest of the reverse, rule: positive anti-code and the original code is the same, The rest is reversed, so-1 of the source is 1 0000001, the anti-code is 1 1111110, and now use the inverse code to calculate 1+ (-1)

0000 0001

+ 1111 1110

————————

1111 1111 ....... Re-converted to the original code is 1000 0000 = 0, although the inverse code to solve the problem of subtraction, but also brought a problem,-0, since 0000 0000 means 0, then there is no-0 of the need to appear +0= -0=0, a 0 is enough, in order to avoid the problem of two 0, computer gurus and Invented the complement, complement rules: the complement of the whole is its own, the complement of negative numbers for its anti-code plus one, so, negative conversion to anti-code to two steps, first, converted to anti-code, the second: the Anti-code plus one. So-1 of the complement is 1111 1111, 1+ (-1)

0000 0001

+ 1111 1111

________________

1 0000 0000 ....... ........... This becomes 9 bits, because char is 8 bits, the highest bit 1 is discarded as 0, the operation is correct

   again,-0: Original code 1000 0000 of the complement is 1 0000 0000, because char is eight bits, so low eight-bit 00000000,   +0: The original code is 0000 00000&NB SP, the complement is also 0000 0000, although the complement of 0 are the same, but there are two 0, since there are two 0, and 0 is neither positive, nor negative,  with the original code of 0000 0000 means that the line,  this, signed char  The original code is used to denote the number between the -127~127, only the original code 1000 0000 is not used, with a combination of permutations can also be calculated, 0??????? , can represent the number of 2^7=128, just is 0~127, 1???????, can also represent 128 numbers, a total of signed Char has 256 numbers, which is two 0 in the middle of -127~127 coincides with exactly. Now let's talk about the rest of the 0000,

since 127 ~0~ 127 have corresponding source code corresponding to it, then 1000 0000 What is it, of course 128, why is-128, someone online said-0 that 1000 0000 and 128 of the same complement, so 1000 0000 means 128, this I really do not agree, or 128 No original code, only the complement of 1000 0000, nonsense, since there is no original code what to complement, there is said-128 of the original code and 0 (1000 0000) The original code is the same, so you can use 1000 0000 to represent -128, I can only say, the answer is not so far-fetched, the original code 1000 0000 and 128 of the original code is actually different, but why can use it to represent-128 to perform the operation, If you do not limit to char type (that is, do not restrict to 8 bits), then, 128 of the original code: 1 1000 0000, 9 bits, the highest bit sign, and then its anti-code:1 0111 1111, and then, the complement is: 1 1000 0000, which is-128 of the complement, Found the same as the original code, 1 1000 0000 and 1000 0000 The same? If the same person is blind, so,-128 of the original code and 0 (1000 000) The original code is different, but in the char type, can be 1000 000 to represent-128, the key is Char is 8 bits, it drops the highest bit of the-128 bit 1, after truncation-128 of the original code is 1000 000 and 0 of the original code is the same, which means

1000 0000 and 128 discard the highest bit after the remaining 8 bits are the same, so you can use-0 to indicate-128, so that the original remaining 0 (1000 0000), is taken to indicate truncated-128, because even after the truncated-128 and the other number of char-type range ( -127~127 Operation does not affect the result, so you can say so-128.

such as -128+ (-1)

0000------------------Discard the highest bit-128

+ 1111 1111------------------1

________________

10111 1111------------------Char to take eight bits, so the result is not correct, but it doesn't matter, the result-129 would have been beyond the char type, of course, can not be expressed.

Like -128+127.

1000 0000

+ 0111 1111

————————

1111 1111---------------1 result is correct, so this is why 1000 0000 is used to represent-128.

Thus is also why Char is -128~127, not -127~127, short int is also so -32768~32767 because in 16 bits, 32768 is the original code for 17 bits, discarding the highest bit remaining 16 is the same as 0 of the original code ....

There is one more question:

Since-128 the highest bit was discarded. So

Char a=-128; It is stored in the complement of 1 1000 0000 in memory, but because it is char, only 1000 0000 is stored

printf ("%d", a); Since the highest bit is discarded, the output should be 1000 000 of the original code of the decimal number-0, but why can output-128 it.

can also print out-128;

I guess is a convention inside the computer, like float, can use 23 bits to represent 24 bits of precision, because the highest bit default is 1, the time to remove 23 bits plus 1.

128 is also the same principle, when the data bus from memory is taken out, the CPU will add the highest one, to 1 1000 0000 so that can be translated into

-128 output, or 1000 0000 How to output? This is, of course, one of my inferences, specifically how to achieve the CPU has to ask the designer ....

Let's look at an example:

Char a=-129;

printf ("%d", a);    How much will it be entered?? The result is 127, why?

129 in the complement of 10 0111 1111 only after the eight-bit storage, that is, 0111 111 This value is exactly 127, the same as 130 after truncation is 126 ....

So by the mode of reincarnation, about the mold is not discussed first.

So

unsigned char a=-1;

if (1>a) printf ("Greater than");

Else

printf ("less than");

What was the result? The surprise is: less than, not greater than, what's wrong with you, or storage problems:

A is unsigned unsigned, its eight bits are used to store the value, no sign bit, the compiler converts 1 to a complement of 1111 1111, but because it is unsigned, the computer will treat 1111 11111 as unsigned, nature is 2^8-1 = 255, so the equivalent Yes if (1>255) must be

printf ("less than");

。。。。。。。。。。。

All right, here it is ...

Analysis of why the range of char types is -128~+127

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.