Article Directory:
First, look at the c language integer data type range
注意:数值范围没有负数的则为无符号位的数,有负数范围的则为有符号位的数
Output:
% U unsigned decimal integer
% d signed decimal integer
% 0 unsigned octal
% x unsigned integer hexadecimal
data are stored in the computer in the form of a complement, the unsigned output U%, no sign bit is positive;% d signed output signed bit符号位在最高位
.
1. There are no conversion symbols and the number of symbols (the same word length)
- We often use the C language casts, sometimes mandatory type conversion is not the result we want, because computer storage data is
补码
stored in the form. - Sometimes the cast may change the value, type of data may be due to whether the sign bit.
- No sign bit is a positive number would not be converted because the original code = positive numbers complement
- Bit signed number, according to the sign bit is a 1 or 0 to determine whether the conversion, then if the sign bit is 1, necessary conversion is negative, the value of course be different.
- Now we look at some code to see if this particular case:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
short x=-4321;
unsigned short y=(unsigned short)x;
printf("x=%d,y=%u\n",x,y);
return 0;
}
- We can see that the value of x and y values have nothing to do, we will convert it into binary, we know why. Let us look at the binary conversion table;
I tested on my computer a bit, because the computer is 64, so the conversion is 64 binary numbers, as to why 16 left all numbers are 1, which relates to the sign extension, details You can refer to my previous article sign extension
- Removing the sign bit ASCII to the original code is shown below:
- Wherein x is a complement, y is true unsigned binary value, n = number of the complement of the original code. Since the number is stored in the computer in the form of a complement, the complement itself is positive, negative complement needs to be converted, particularly with reference to the conversion of the original code, anti-code, complement, shift conversion rule
- unsigned short unsigned integer, so it is not the sign bit, bit all values are positive.
- And is a signed short integer with a sign bit, the sign bit in the most significant bit, it is necessary to convert the original code to the complement output.
- As can be seen, casts result value corresponding to the same position, changing only the interpretation of these bits embodiment, explanation is short, unsigned short or explanation, these two methods.
We look at the same piece of code carefully try to figure out try to figure out:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
unsigned short x=65535;
short y=(short)x;
printf("x=%u,y=%d\n",x,y);
return 0;
}
- Respective position value are equal, but illustrating the results are different, because it is interpreted in different ways; unsigned short short and interpreted in two ways.
2. The transition between the different integer word length
(1) variable length characters to cast small print variable length
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int x=165537,u=-34991; //int 4B
short y=(short)x,v=(short)u; // short 2B
printf("x=%d,y=%d\n",x,y);
printf("u=%d,v=%d\n",u,v);
return 0;
}
- When characters long variable to cast small print variable length, the system will direct the excess part of the upper word length truncation rounding, low direct assignment
(2) small print casts to long variable characters long variable
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
short x=-4321; // short 2B
int y=x; // int 4B
unsigned short u=(unsigned short)x;
unsigned int v=u;
printf("x=%d,y=%d\n",x,y);
printf("u=%u,v=%u\n",u,v);
return 0;
}
- Here we converted to hexadecimal output
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
short x=-4321; // short 2B
int y=x; // int 4B
unsigned short u=(unsigned short)x;//无符号数,直接取当前数据类型长度的原数字x的补码长度,不用转换成原码输出
unsigned int v=u;
printf("x=%d,y=%d\n",x,y);
printf("u=%x,v=%u\x",u,v);
return 0;
}
- We found that short wordlength integer-to-integer conversion word length, not only to make equal the corresponding position
高位部分还会扩展为原数字的符号位
. - Note: char types integer of 8-bit ASCII code, to int, the upper portion to 0s. A 7-bit binary ASCII code digits.