C language of unsigned int / char we in the end how to understand and distinguish ????

EDITORIAL: Here is Wang log growth, an ordinary college students, I would like to learn and to share their own study notes out, record their growth trajectory, and who might need help, usually blog is primarily aimed at some of the study notes (the system will be included with mind mapping, like a column before I - data Introduction to Science , like), project combat notes, some of the technology and explore some of their own thinking. Welcome attention to every comment your point of concern I would like to see it carefully. Have any questions please exchange, I will do my best to help you, create a better environment CSDN.



1. Pre-knowledge

The original code, anti-code complement


Here on the Central Plains computer code, anti-code and complement the knowledge of space restrictions, here is a brief introduction, a more detailed explanation will send me two days to sort out


Original code : original code is a number in the computer simplest form expressed by a sign bit (0 positive 1 negative) and the bit values (absolute value) composed of

  • But will have two 0 : +0 ( 0 000 0000) and -0 ( 1 000 0000)

Anti-Code : original code except the sign bit bitwise (0 and 1 interchanged), only the negative sense , positive or their anti-code

E.g:

Original code Inverted
0000 1111 0000 1111
1000 1010 1111 0101

Complement : anti-code plus 1, or only on the negative meaningful , positive or complement your own. The negative complement its expression in the form of a computer
such as (to -5 for example):

The original code (bits + sign bit value) Inverted (except the sign bit bitwise) Complement (inverted +1)
10000000 00000000 00000000 00000101 11111111 11111111 11111111 11111010 11111111 11111111 11111111 11111011
  • In fact, it is convenient to complement the computer subtraction exist in the computer, plus minus a number equal to a number of complement, which involves the concept of "model", interested or do not understand can go to Baidu Encyclopedia look

    Reference :
    Encyclopedia - inverted
    Wikipedia - complement
    Baidu library

2. signed and unsigned

There are signed and unsigned main difference

  • Unsigned numbers in the computer represented by the original code, represented by a signed number complement

  • Like their name suggests difference lies in whether the symbol

    • Unsigned number bits are used to represent all the values
    • The original code symbols and have anti-complement code as mentioned above, by the sign bit (MSB 0 1 positive negative) and value bits

They represent a range of different:

  • * In the n-bit unsigned number, since all the bits are used to represent the values, so its range is directly 0-2 n-. 1) ** (* a total of 2 n distinct numbers)
  • In a signed number, since the highest bit is used to represent the positive and negative values, it can be represented by its maximum value will shrink, but will be extended in the axial direction of the negative

As follows:

Unsigned Signed
Single byte (8 bits) 0~255 -128~127
Bytes (16 bits) 0~65535 -32768~32767
n bits 0~ 2^n - 1 - 2^n ~ 2^n - 1
  • In fact, you can feel it, have signed and unsigned in the same number of digits of their number capable of expressing the number is consistent
    • However, complement +0 = -0, therefore, -0 this position can empty out, so that in the more negative in a region from the original -0 ~ -127, -128 -1 to become also or 128, and the positive direction is 0 to 127, the number is 128, the two are symmetrical. (Own complement of -0 can try to take down)

      Original code Inverted Complement
      1000 0000 1111 1111 1000 0000
    • Also can be understood:

      • We think, in the -1 to -128, -1, is the largest, so -1 is the biggest negative value in this context, that is, 1 111 1111
      • For we 11111111-1 value of a value of 1111 1110 2 (from their complement can be pushed back to the original code)
      • Such has been cut down, down to the smallest negative value is -128 1000 0000
    • Such negative direction is -1 to -128, the same positive direction

    • Reference: C Language Series (b) signed and unsigned Comments )

Binary (1 byte) Decimal value
1111 1111 -1
1111 1110 -2
1111 1101 -3
……… ………
1000 0001 -127
1000 0000 -128

3. signed char and unsigned char

  • First, we must know the type of plug ** char in the C language is only a byte (8) ** of
  • Secondly, in the C language, if we pay attention, we are able to discover char character type of storage is actually stored character ascii code , so sometimes we direct subtraction is also possible for characters and integers.
 	  int main(void)
                {
                    char ch = 'c';
                    printf("%d",ch);//打印结果是99,为'c'的ancii码
                    printf("%c",ch+5);//打印结果为h
                }
  • But in the C language as well as unsigned char not worry this thing
    is actually quite simple, unsigned char in C language can be expressed in the range of 0 to 255, with the average number of unsigned is the same,
    while the average char type can be represented by a range of -128 to 127, which has the same number of symbols is

  • Finally, with regard to the scope represented here in fact generally only at the time of operation and the integer is to be noted , but when we do not need to represent characters too concerned

4. Reference:

  • See text hyperlinks

Have seen here, you brothers and sisters, aunts and uncles give Wang a point off a note like it leave a message, and Amy grow up with it, your attention is my greatest support.


If there is more than content at any inaccuracies or omissions, or you have a better idea, just leave a message below to let me know - I'll do my best to answer.

Published 19 original articles · 87 won praise · views 4717

Guess you like

Origin blog.csdn.net/weixin_45761327/article/details/105300787