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 integer type), so the int type 32 bits is-(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 as 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 right (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 = 255.

 

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 can represent the value of the left (n-1) bit, such as char a =-1; Then the binary representation is 1 0000001,1 is represented as 0 0000001, so the signed char drop symbol bit the remaining 7 bits maximum is 1111 111 = 127, and then add the symbol, 0 1111111 = 127,1 111111 1 = 127, the range should be -127~127, the same kind of int type, 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 values, unsigned integers are saved with all Storage , signed integer, the highest bit as the symbol bit, the rest is to represent the value, this seems reasonable, but brings 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, so 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 counter, the provisions: Positive inverse code and the original code is the same, negative number of the inverse code is the original code in addition to the symbol bit, 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 is necessary, +0 = 0 = 0, a 0 is enough, in order to avoid the problem of two 0, count Computer masters have invented the complement, complement rules: the complement of the whole is itself, the complement of negative numbers for its anti-code plus a, so, negative conversion to anti-code to two steps , the first, 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 the result is 0, and the operation is correct.

look again,-0: Original code 1000 0000 is the complement of 1 0000 0000, because char is eight bits, so take low eight bit 00000000, +0: The original code is 0000 00000 , the complement is also 0000 0000, although the complement 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 is the line.


In this way, signed char, the original code is used to denote the number of -127~127, but the remaining original code 1000 0000 is no use, with a combination of permutations can also be calculated, 0??????? , can represent 2^7 = 128 numbers, just the 0~127. 1???????, can also represent 128 numbers, a total of signed Char has 256 numbers, which coincides with two 0 in the middle of -127~127.


Now let's talk about the rest of the 0000,since 127 ~ 0 ~ 127 have corresponding original code corresponding to, then 1000 0000 What is, of course, 128, why is-128, online people say-0 that1000 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 (100 0 0000) The original code is the same, so you can use 1000 0000 to indicate -128,I can only say that the answer is not so far-fetched, the original code 1000 0000 and 128 of the original code is actually different, but why you can use it to represent-128 to perform the operation,If you do not limit to char type (that is, do not qualify as 8-bit), then see, 128 of the original code: 1 1000 0000, 9-bit, the highest bit sign, and then calculate its inverse code:1 0111 1111, in turn, the complement is: 1 1000 0000, which is-128 of the complement , found 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, it can be 1000 000 to represent-128, the key is that char is 8 bits, it put 128 of the highest bit sign bit 1 discarded , after truncation-128 of the original code is 1000 000 and 0 the same as the original code, which means1000 0000 and 128 discard the highest bit after the remaining 8 bits are the same, so can be used-0 means -128, so that the remaining 0 (1000 0000), was taken to indicate the truncated -128,because even the truncated 128 and other number of char range ( -127~127) operations do not affect the result, so dare to 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, naturally 2^8-1 = 255 , so the equivalent of if (1>255) is definitely printf ("less than");


Translated from: Http://blog.csdn.net/daiyutage

Copyright NOTICE: This blog post, mostly I compiled, or in the network collection, reproduced please indicate the source!!

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.