Let's take a look, C, the difference between the unsigned and signed two forms for the integer type (integer form), and how it is stored in memory
The integer type (integer form) is one of the basic data forms in the C language, what exactly is the definition of an integer? Look at the book, in the introduction of the integer, there is a key word is size (range)
The C programming Language wrote a integer,typically reflecting the natural size of integers on the host machine
Programming in ANSI C wrote integers is whole numbers with a range of values supported by a particular machine.
So, the integer is all integers in a range, but the scope is for a particular computer (depends on computer).
Before you begin, get to know the basics-the amount of memory that your computer's storage units and integers store on your computer.
1). The smallest storage unit of a computer is a bit or binary digits, which is used to hold a binary number, or 0 or 1. 8 bits for one byte byte.
2). For 16-bit (16-bit) computers, int is stored in two bytes, while 32-bit's computer is stored in 4 bytes, or 32 bits.
If you want to understand the difference between singed and unsigned, in addition to these two basic knowledge, but also need to know how to store integers in the computer, to 16-bit computer as an example, the definition of int a = 1; So the storage of a is represented by a table.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
The first thing to mention is that in the C language, decimal integers are converted to binary storage on the computer. Continue, the above declared int a = 1, that is, int signed a =1,c language default A is a signed type. The top left of the table above is the highest, and the lowest of the right is the least bit. An integer of type signed, which is encoded only by removing the highest bit, the remaining 15 bits, while the highest level is used to mark (sign), mark the positive or negative of an integer, 0 means positive, and 1 means negative. So for signed integers, the storage range is ( -2^15 to 2^15-1), which is an integer of 32768 to +32767.
For unsigned integers, the 16 bits are all encoded, and the storage range is (0 to 2^16-1), which is a non-negative integer from 0 to 65535. So you can declare int a = 1, or int a =-1, but you cannot declare unsigned a =-1. But one thing to mention is that, regardless of whether the integer type is signed or unsigned, it is stored in 16 bits, which means that 16 bits are all used to store the data
As seen above, the a=1 is stored by converting the decimal A into a 2-byte 2 binary on the programmer's calculator, and then placing the result in the table above. (Original code storage)
But how is the int a =-1 stored? So what is the way to store negative numbers?
Negative numbers are stored in the form of the complement of the original code, as shown in the table below.
Original code
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
Anti-code
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
The inverse code is to change the binary 1 in the original code to 0, the binary 0 to 1. After the anti-code is obtained, on the basis of the anti-code plus 1.
Complement
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
Do not know, here, we will not have a doubt, the above-mentioned signed and unsigned range is the intersection, that is, all contain 0 to +32767 of the range of integers, then this range of integers in the declaration as signed and unsigned, the same way of storage? The answer is yes, in this range of integers, signed and unsigned are stored the same way.
Just started to learn C, I do not know how to understand these coding, storage means what is the point, I now personally feel, only in the declaration of int, if not guarantee its positive integer, do not declare as unsigned, of course, can guarantee the premise of declaring as unsigned, indeed expanded the scope. http://chuansu.iteye.com/blog/1435150
The difference between unsigned and signed