signed和unsigned有什么不同??

fan_dtor 2002-11-30 02:22:52
定义
int a=-1;
unsigned int b=-1;
printf("%d %d ",a,b);
printf("%u %u",a,b);
结果:
-1 -1 65535 65535
那signed和unsigned还有什么不同??
a和b在内存中的怎样显示?1111111111111111还是0111111111111111??
请高手指点,最好可以举个例子和用二进制说明一下.
...全文
331 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ywpg 2002-11-30
  • 打赏
  • 举报
回复
up
zhang865 2002-11-30
  • 打赏
  • 举报
回复
AGREE
crystal_heart 2002-11-30
  • 打赏
  • 举报
回复
相同的sign和unsign数字在计算机中的二进制表示完全一样。不同的是他们代表的数字的范围。对于unsign来说,没有负数的概念。16位的机子上,十进制数-1的补码表示为1111111111111111B,如果这数字表示为sign就是-1,如果这数字表示为unsign就是FFFFH(65535)。
daniceexiedong 2002-11-30
  • 打赏
  • 举报
回复
每个类型都有自己的用处,和用法。

其实在内存中都一样,主要是外在的表现形式不同。
fan_dtor 2002-11-30
  • 打赏
  • 举报
回复
那我用UNSIGNED和signed的相加结果都一样,打印出来
结果也一样,
如:
unsigned int i=65535;
int a=-1;
printf("%d %u",i+a,i+a);
结果:
-2 65534
那unsigned 和signed用起来还有什么分别?
qing_li73 2002-11-30
  • 打赏
  • 举报
回复
Converting Signed to Unsigned
Objects of signed integral types can be converted to corresponding unsigned types. When these conversions occur, the actual bit pattern does not change; however, the interpretation of the data changes. Consider this code:

#include <iostream.h>

void main()
{
short i = -3;
unsigned short u;

cout << (u = i) << "\n";
}

The following output results:

65533

In the preceding example, a signed short, i, is defined and initialized to a negative number. The expression (u = i) causes i to be converted to an unsigned short prior to the assignment to u.

Converting Unsigned to Signed
Objects of unsigned integral types can be converted to corresponding signed types. However, such a conversion can cause misinterpretation of data if the value of the unsigned object is outside the range representable by the signed type, as demonstrated in the following example:

#include <iostream.h>

void main()
{
short i;
unsigned short u = 65533;

cout << (i = u) << "\n";
}

The following output results:

-3

In the preceding example, u is an unsigned short integral object that must be converted to a signed quantity to evaluate the expression (i = u). Because its value cannot be properly represented in a signed short, the data is misinterpreted as shown.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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