Deep parsing unsigned int and Int_c languages

Source: Internet
Author: User
Tags modifier modifiers

just like int A; int can also be decorated with other modifiers. In addition to the void type, the basic data types can be preceded by various types of modifiers, with the following four types of type modifiers:
1.signed----with symbols, can be decorated with char, int. int is signed by default.
2.unsigned-----unsigned, modifier int, char
3.long------long type, decorated int, double
4.short------Short type, modifier int

Let's take a look at the connection and difference between signed and unsigned and int.
What is called a symbol, and what is called an unsigned
The problem is really simple, for example: 5 and -5,5 have no symbols,-5 have symbols. It's easy. But this symbol on the computer is not simple. Let's take a look at them separately:
Before we can explain the difference between signed and unsigned, we must first know what the overflow is, because there is no sign of the root cause of the data is due to the overflow phenomenon caused.

Overflow:
We know that data is stored in binary form in the computer and occupies a certain space, which belongs to the space allocated by the computer.
The computer allocates 32 or 16 bits of space (different computers may be different) to int, since the space is limited, then the value will have a limit, there will be the maximum and the minimum value of this, for example: assuming the type of 16-bit int, unsigned type of maximum value of 1111 1111 1111 1111 (16 1), that is 65535, if more than 65535, this is called overflow, then what to do? If you want to output 65536, what will it output? Let's take a look at the following:

question: Some readers will ask: 65535 so small ah, I remember myself in the output than 65535 a lot of the number can also output AH.
Answer: That's the reason for the unsigned definition and your computer compiler. 64-bit computers and 32 of computers are not the same. and the int occupies several bytes is related to the computer compiler. But now most of the computer int 4 bytes, that is 32 bits, then his maximum value is 32 1 (binary) about the order of magnitude, have you experimented with such a large number?

1. Unsigned integral type (unsigned int)
(1) We all know that the integer is 4 bytes (some compilers are different, may be 2), that is, 32 bits, unsigned integers of course, 32 bits.
(2) since it is a 32-bit, unsigned integer values are 32 0~32 1, namely: 0~4294967295
(3) Let's give an example: 32 bit is a bit long, so we take the 16-bit unsigned short int for example.
The short int is 16-bit, and the unsigned range is 0~65535
Take the decimal 32767 (all of the following examples take this number for a fact), its binary is:
0111 1111 1111 1111
For unsigned integer 32767来, it says that the highest bit of its binary is called the data bit, that is, that 0 is the data bit, the data bit is to participate in the operation, if we change 0 to 1, that is 16 1, its decimal is 65535 (is 2 15 times +2 14 times side ...) Up to 2 of the 0-time side), which is different from the signed integer type.
(4) in order to understand the meaning of (3), do a program description:

Copy Code code as follows:

#include <stdio.h>
Main ()
{
unsigned short int a=32767,b=a+1;//defines a shortened integer unsigned
printf ("a=%u\nb=%u\n", a,b);//unsigned output
}




The definition of the time a=32767, that is 0111 1111 1111 1111, the output is still 32767,
a+1=32768, the binary is 1000 0000 0000 0000, and the input is still 32768.
According to (3), the unsigned integer binary highest bit is the data bit, the data bit is 0 1 are calculated according to normal.

2. Signed integer ((signed) int) (1) int type is signed by default, so int is actually signed int, we usually omit signed
(2) The signed integer is also 32 bits.
(3) Its value range is different from the unsigned integer type. Its scope is -2147483648~2147483647 this range can be understood as the half of the unsigned integral becomes negative.

The 32 bit is a bit long, so we take the 16-bit short int for example.
The short int is 16-bit, and the signed range is -32768~32767

This time someone may ask a question, 32768 in binary notation for 1000 0000 0000 0000, then this minus 32768 minus again how to understand? Look underneath.
(4) give an example;
Or in 32767 as an example, its binary system is:
0111 1111 1111 1111
For a signed integer 32767来, its binary highest bit is called the symbol bit (not the data bit), and the symbol bit is the name of the sign, the rule: 0 is positive, 1 is negative.
(5) Enumerating the contents of a program understanding (4)

Copy Code code as follows:

#include <stdio.h>
Main ()
{
Short int a=32767,b,c,d;//defines unsigned types.
b=a+1;
c=a+2;
d=a+3;
printf ("a=%d\nb=%d\nc=%d\nd=%d\n", a,b,c,d);
}

We can see that this is the result of this appearance. Why, then? What's going on?
In fact, in the computer, negative numbers do not exist, it is in the form of binary complement to represent and store. What is the complement?

(6) What is the complement, the complement of the operation.
Let's just cite a simple example. Just use-6.

We have learned from the above that the negative sign bit is 1, so:
(1)-6 of the binary: 1000 0000 0000 0110 (called the original code, the original code is shown to me by the computer)
(2) to the original code for the reverse code: 1111 1111 1111 1001 (called the Inverse Code, keep the symbol bit unchanged, the original code in the 0 variable 1,1 to 0)
(3) to the counter code plus 1:1111 1111 1111 1010 (called the complement, the complement is stored in the computer in the form of negative numbers)
In a computer, if the stored binary is 1111 1111 1111 1010, then the decimal number displayed in our foreground is-6. That is, negative numbers are stored in the computer in the form of the binary complement of the negative number.

(7) Understand what is the complement, and then look at the above we said that the program:
The binary of 32767 is: 0111 1111 1111 1111
Let's figure out why the value of C equals-32767.
The binary of c=32767+2,c is: 1000 0000 0000 0001 (32767 binary +2), this binary of C is the complement that is stored in the computer, need to convert it to the original code, that is, the binary of C is reduced repeatedly. The resulting binary source code is: 1111 1111 1111 1111. As we have said, the sign bit is 1, which means negative value and does not participate in the operation, so this binary decimal is:-32767.
However, in the above, C's original code is indeed 1111 1111 1111 1111,c stored in the computer's complement is indeed 1000 0000 0000 00010. But the origin of the 32767 has another understanding, the complement of C is 16 bits, 32-bit compiler has 32-bit binary, that is, the 16-bit complement in front of the (32-16=16) bit of the number of virtual digits, does not belong to the computer to the short int allocated space, However, these 16-bit digits are 0 when the number is positive, and 1 when the number is negative. and the first 16 digits are all consistent with the binary 8th digit. Other words:
The complement of C is 1...1 1000 0000 0000 0010 (1..1 means 16 1)
We can calculate this:-2 of the 7 times +2 of the 1 =-32767, this understanding is generally accepted by the public, and avoid the concept of the original code.

(8) Through the program can also find a law, the value range of int is -32768~32767, the head tail connected to form a ring on it.

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.