c 有符号与无符号之间的转换

zgw2014 2017-07-27 03:34:08
这是几道题目,本人初学,有些迷糊,下面是我在自己安装的虚拟机里边运行的结果:,实在整不明白咋回事,到底在什么情况下该转换类型,什么情况下不该转换类型?很迷糊,求各位论坛的大牛们指点指点小弟,万分感谢
...全文
512 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-28
  • 打赏
  • 举报
回复
电脑内存或文件内容或传输内容只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存或文件内容或传输内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息…… http://edu.csdn.net/course/detail/2344 C语言指针与汇编内存地址-一.代码要素
赵4老师 2017-07-28
  • 打赏
  • 举报
回复
http://edu.csdn.net/course/detail/2344 C语言指针与汇编内存地址-一.代码要素
赵4老师 2017-07-28
  • 打赏
  • 举报
回复
无对应汇编指令,不分析。 仅供参考:
#include <stdio.h>
unsigned short int ui;
  signed short int si;
int main() {
	ui=(unsigned short int)0x8000u;
	si=(  signed short int)0x8000;
	printf("ui=%u\n",ui);
	printf("si=%d\n",si);
	ui=ui>>1;
	si=si>>1;
	printf("ui=%u\n",ui);
	printf("si=%d\n",si);

	printf("--------------\n");
	ui=(unsigned short int)0x8000u;
	si=(  signed short int)0x8000;
	printf("ui=%u\n",ui);
	printf("si=%d\n",si);
	ui=((  signed short	int)ui)>>1;
	si=((unsigned short	int)si)>>1;
	printf("ui=%u\n",ui);
	printf("si=%d\n",si);

	return 0;
}
//ui=32768
//si=-32768
//ui=16384
//si=-16384
//--------------
//ui=32768
//si=-32768
//ui=49152
//si=16384
zgw2014 2017-07-28
  • 打赏
  • 举报
回复
各位能不能就上边我提问题的截图里边的比较运算随便拿一个出来分析讲解一下为什么会是那个结果?我现在满疑惑的
不懂啊不懂 2017-07-27
  • 打赏
  • 举报
回复
你写的所有代码在内存中都是0和1,编译器也不认识它是不是有无符号,只有你告诉编译器他是有符号的或者无符号的,否则都是0和1的运算 至于你不知道什么时候转换,那你操心这些有设么用,等你需要大小比较时,自己就会知道什么时候要转换了,就是说你认为它不转换就是错的,你就可以转换了
自信男孩 2017-07-27
  • 打赏
  • 举报
回复
一个数值在内存的单元里存放都是二进制形式,二进制值都是一样的,只是要看你用什么形式输出。 打个比方,在内存里存放的是0x11,%d输出的可能是-1, %u输出的可能是3(仅仅是打个比方);
战在春秋 2017-07-27
  • 打赏
  • 举报
回复
引用
在什么情况下该转换类型,什么情况下不该转换类型
强制转换类型取决于程序员,你可以任意转换不同数据类型。 但不同数据类型能存放的数值有上下限,如果等号右侧的数值超过等号左侧类型能存放的界限。 转换时会有截断,不能保证转换成功。
long int i = 123456789997787878;
int j = (int)i;  //j不会等于123456789997787878
加入符号表示后情况也是一样,到底这个数值是多少,要看本质上 二进制各位的值
赵4老师 2017-07-27
  • 打赏
  • 举报
回复
注意:常量也有类型! C++ Integer Constants See Also Send Feedback Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Grammar integer-constant: decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant: nonzero-digit decimal-constant digit octal-constant: 0 octal-constant octal-digit hexadecimal-constant: 0xhexadecimal-digit 0Xhexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit: one of 1 2 3 4 5 6 7 8 9 octal-digit: one of 0 1 2 3 4 5 6 7 hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix: unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix: one of u U long-suffix: one of l L 64-bit integer-suffix: i64 LL ll To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: Copy Code int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: Copy Code int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the "x" does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: Copy Code int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: Copy Code unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value To specify a 64-bit integral type, use the LL, ll or i64 suffix. For example, Copy Code // 64bitsuffix.cpp #include <stdio.h> enum MyEnum { IntType, Int64Type }; MyEnum f1(int) { printf("in f1(int)\n"); return IntType; } MyEnum f1(__int64) { printf_s("in f1(__int64)\n"); return Int64Type; } int main() { MyEnum t1 = f1(0x1234), t2 = f1(0x1234i64); } Output in f1(int) in f1(__int64) See Also Concepts Literals (C++)
sdghchj 2017-07-27
  • 打赏
  • 举报
回复
字面量数字默认是有符号的。 有符号与无符号数字进入运算时,默认是把有符号自动转为无符号。 什么时候该用强制转换,得看你的意图是什么。首先要搞明白负数是如何存储与表达的。 对于计算机而言,全是二进制,它不管你正还是负,得看你怎么去解析它。 以单字节为例,一个字节能表示的范围是0x00~0XFF共256个数字,如果你认为它是无符号的,它的范围就是0~255;如果你认为它是有符号的,它的范围就是-128~127。注:负数是用补码存储。 127的二进制:0111 1111 如果再加1就成了二进制:1000 0000 这个时候如果你认为它是有符号的,它就是-128,无符号的就是128。 也就是说,00000000~0111 1111对于有符号和无符号都是表示0~127,而1000 0000 ~11111111对于有符号而言是-128 ~ -1,对于无符号而言是128~255。 于是不难看出char类型的-1跟unsigned char的255相等。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧