(reprinted) in-depth parsing of unsigned int and int

Source: Internet
Author: User
Tags modifiers

Just like int A, int can also be decorated with other modifiers. In addition to the void type, the base data type can be preceded by a variety of type modifiers, such as the following four types of modifiers:
1.signed----Signed, can be decorated with char, int. int is signed by default.
2.unsigned-----unsigned, modifier int, char
3.long------long type, modifier 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 signed, what is unsigned
The problem is very simple, for example: 5 and -5,5 are unsigned,-5 is signed. It's simple. But this kind of symbol in the computer is not easy. Let's take a look at each:
Before we explain the difference between signed and unsigned, we must first know what the overflow is, because there is no sign of the root cause can be said because the data overflow phenomenon.

Overflow:
We know that data is stored as binary in the computer and occupies a certain amount of space, and this space belongs to the space allocated by the computer.
The computer assigns an int 32 or 16 bits (different computers may be different) space, since the space is limited, then there will be a limit to the value, there will be a maximum and minimum value of the said, such as: Assume that the int type is allocated 16 bits, the maximum value of the unsigned type is 1111 1111 1111 1111 (16 1), or 65535, if it exceeds 65535, which is called overflow, what should I 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, I remember in the output than 65535 a lot of the number can also output AH.
Answer: That's why there's an unsigned definition and your computer compiler. 64-bit computers and 32 of computers are different. And a few bytes of int are related to the computer compiler. But now most of the computer int account for 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 type is 4 bytes (some compilers are different, maybe 2), 32 bits, unsigned integers, and of course 32 bits.
(2) since it is 32 bits, the value of unsigned integer is 32 0~32 1, namely: 0~4294967295
(3) Let's give an example: 32 bits 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 examples below for this number), its binary is:
0111 1111 1111 1111
For unsigned integer 32767来, the highest bit of its binary is called the data bit, that is, the 0 is the data bit, the data bits are to participate in the operation, if we change 0 to 1, that is 16 1, its decimal is 65535 (2 15 of the +2-time Square). is added to 2 of the 0-time Square), which is different from the signed integral type.
(4) in order to understand the meaning of (3), do a program description:

Copy CodeThe code is as follows:
#include <stdio.h>
Main ()
{
unsigned short int a=32767,b=a+1;//definition of 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.
As explained in (3), the binary highest bit of the unsigned integer is the data bit, and the data bit is 0 1 is calculated according to the normal.

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

The 32 bit is a bit long, so let's take a 16-bit short int for example.
The short int is a 16-bit, signed range of -32768~32767

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

Copy CodeThe code is as follows:
#include <stdio.h>
Main ()
{
The short int a=32767,b,c,d;//defines the unsigned type.
b=a+1;
c=a+2;
d=a+3;
printf ("a=%d\nb=%d\nc=%d\nd=%d\n", a,b,c,d);
}

It can be seen that the results at this time are actually this way. Why is it? What's going on?
In fact, in the computer, negative numbers do not exist, it is in the form of twos complement representation and storage. 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 through the above learning that the sign bit of negative numbers is 1, so:
(1)-6 binary: 1000 0000 0000 0110 (referred to as the original code, the original code is shown to me by the computer)
(2) The original code to reverse code: 1111 1111 1111 1001 (called Anti-code, keep the symbol bit unchanged, the original code of 0 to 0)
(3) Anti-code plus 1:1111 1111 1111 1010 (called the complement, the complement is stored in the computer negative form)
In the computer, if the stored binary is 1111 1111 1111 1010, then the decimal number displayed in our foreground is-6. That is, a negative number is stored in the computer as a binary complement of the negative number.

(7) After understanding what is the complement, then look at the program we said above:
The 32767 binary is: 0111 1111 1111 1111
Let's figure out why the value of C is equal to-32767.
The binary of the c=32767+2,c is: 1000 0000 0000 0001 (32767 binary +2), the C binary is the complement stored in the computer, it needs to be converted to the original code, that is, C's binary number minus repeatedly reversed. The resulting binary source code is: 1111 1111 1111 1111. As we have already said, the sign bit is 1, which means negative values and does not participate in the operation, so the decimal of this binary is:-32767.
However, in the above, the original code of C is indeed 1111 1111 1111 1111,c the complement stored in the computer is indeed 1000 0000 0000 00010. However, the origin of the 32767 has another understanding, C is the complement of 16 bits, 32-bit compiler has 32-bit binary, that is, in front of the 16-bit complement and (32-16=16) bit of the virtual number of bits, is not a computer to short int allocated space, However, the 16-bit digits are 0 when the number represents a positive time, and 1 when the number is negative. and the first 16 digits are all identical to the number 8th digit in the binary. Other words:
C's complement is 1...1 1000 0000 0000 0010 (1..1 = 16 1)
We can calculate this:-2 of the 7 square +2 of the 1 =-32767, this understanding is generally accepted by the public, and avoids 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 is connected together to form a ring on it.

(reprinted) in-depth parsing of unsigned int and int

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.