C问题我对这个概念非常困惑,请帮助我解决这个问题 [英] C Problem i am very confused about this concept please help me anybody about this problem

查看:69
本文介绍了C问题我对这个概念非常困惑,请帮助我解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned int i=65535;
int j;
j=i;
printf("%d",j);
output

-1   //// Why the output of j=-1 ....Please explain this


int j=-2;
unsigned int i=1+j;
printf("%u",i);
output

65535   //// Wht the output of i=65535

推荐答案

两个评论都是正确的,我只想添加一些额外的资源和更多评论:

任何变量都有最大和最小允许值.请参阅此表: http://msdn.microsoft.com/en-us/library/296az74e.aspx [ ^ ].

如果变量可以从-32768到32767,并且您将一个值放入变量内,那么您将发生溢出,并且将根据情况进行处理,最后,数字等于位和表示形式(必须表示符号),因此根据您使用的容器的类型,某些位可能意味着一件事或另一件事.

请查看此链接以获取更多信息: http://en.wikipedia.org/wiki/Integer_overflow [ ^ ]

祝您好运!
Both comments are right I just wanted to add some extra resources and a little bit more of comments:

Any variable has a maximum and a minimum allowed. See this table: http://msdn.microsoft.com/en-us/library/296az74e.aspx[^].

If a variable can go from -32768 to 32767, and you are putting inside the variable a value out of bounds, then you will have an overflow and it is handled depending on the case, at the very end, a number is an amount of bits and a representation (sign must be represented) so depending on the kind of container you are using some bits can mean one thing or another one.

Take a look at this link for more information: http://en.wikipedia.org/wiki/Integer_overflow[^]

Good luck!


这并不是真正的溢出,您可以简单地将N位整数视为有符号和无符号.如果使用整数作为有符号数,则N位整数可以将值存储在[-2 ^(N-1).. 2 ^(N-1)-1]范围内,如果将其用作无符号数,则其值范围为[0 ..(2 ^ N)-1].无论使用哪种整数,范围[0 .. 2 ^(N-1)-1]在两种情况下都是相同的.
阅读本文以了解我的意思,相同的整数值及其位可以解释为有符号或无符号整数:
http://en.wikipedia.org/wiki/Two%27s_complement [
This is not really overflow, you can simply treat an N bit integer as signed and unsigned. If you use the integer as signed then the N bit integer can store values in the range [-2^(N-1) .. 2^(N-1)-1] if you use it as unsigned then it value range is [0 .. (2^N)-1]. No matter which way you use that integer, range [0 .. 2^(N-1)-1] is the same in both cases.
Read this article to understand what I mean, the same integer value and its bits can be interpreted as either a signed or an unsigned integer:
http://en.wikipedia.org/wiki/Two%27s_complement[^]

also try this:
int i;
unsigned int ui;
for (i=-5; i<=5; ++i)
{
    ui = (unsigned int)i;
    printf("%2d %2d %2u %2u %04x %04x\n", i, ui, i, ui, i, ui);
}


运行这段代码后,您将看到i和ui的十六进制值(二进制表示)始终相同,只有printf函数和生成的汇编代码将整数的位区别对待.


也尝试以下代码:


After running this piece of code you will see that the hex value (binary representation) of i and ui are always the same, only the printf function and the generated assembly code treats the bits of the integer differently.


Try this code as well:

int i;
for (i=0; i<256; ++i)
{
    printf("%d%d%d%d%d%d%d%d %02x %3u %4d\n", i&0x80?1:0, i&0x40?1:0, i&0x20?1:0, i&0x10?1:0,
        i&0x8?1:0, i&0x4?1:0, i&0x2?1:0, i&0x1?1:0, i, (unsigned char)i, (signed char)i);
}


这段代码打印从0x00到0xFF的字节值,并且始终将您打印出来,如果您将这些位解释为无符号和有符号值,则会得到十进制值.查看负数!最高和最低的负数在哪里?如果从当前值为0的整数中减去1,然后将其打印为二进制,有符号或无符号格式,会发生什么情况?这是溢出的主题出现的地方...


This piece of code prints byte values from 0x00 to 0xFF and it always prints you which decimal value do you get if you interpret those bits as unsigned and signed values. Check out the negative numbers! where is the highest and the lowest negative number? what happens if you subtract 1 from an integer whose value is currently 0 and you print it as a binary, signed or unsigned format? This is where the topic of overflow comes in...


这篇关于C问题我对这个概念非常困惑,请帮助我解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆