做`mod'时有符号和无符号的区别 [英] The difference between signed and unsigned when doing `mod'

查看:99
本文介绍了做`mod'时有符号和无符号的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我不明白为什么会发生这种情况?

代码1将输出`fff9''

和代码2将输出'1''

'mod 8'怎么没有效果?


/ *代码1 * /

#include< stdio.h>

#include< stdlib.h>


int main(int argc ,char * argv [])

{

unsigned short a,b,c;

a = 0;

b = 7;


c =(ab)%8;


printf("%x \ n",c);

返回0;

}


/ *代码2 * /

#include< stdio .h>

#include< stdlib.h>


int main(int argc,char * argv [])

{

unsigned short a,b,c;

a = 0;

b = 7;


c =(ab)%8U;


printf("%x \ n",c);

返回0;

}

解决方案

In文章< 11 ********************** @ z14g2000cwz.googlegroups .com> ;,

Hanzac Chen< ha *** *@gmail.com>写道:

/ *代码1 * /
#include< stdio.h>
#include< stdlib.h>


注意:你实际上并没有在你展示的任何代码中使用stdlib.h。

stdlib.h虽然是EXIT_SUCCESS的来源,你可以使用

作为你的返回代码,而不是使用幻数0.


int main(int argc,char * argv [])
{
无符号短a,b,c;
代码1将输出`fff9''


如果未签名的短款恰好与机器上的大小不同

您碰巧测试它。

a = 0;
b = 7;

c =(ab)%8;
printf("%x \ n",c);


printf%x格式需要unsigned int,而不是unsigned short。

可能会有一些默认参数提升,但

'会影响结果。

返回0;
}



-

如果你撒谎到编译器,它将报复。 - Eric Sosman


ro **** **@ibd.nrc-cnrc.gc.ca (Walter Roberson)写道:

文章< 11 *************** *******@z14g2000cwz.googlegroups .com> ;,
Hanzac Chen< ha **** @ gmail.com>写道: / *代码1 * /
的#include< stdio.h中>
的#include< stdlib.h中>

stdlib.h是EXIT_SUCCESS的源代码,你可以将它作为返回代码而不是使用幻数0。




但是0是所有数字中最不神奇的,并且标准保证

返回0; "在main()中返回一个表示成功的状态。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


2005年10月19日19:49:04 -0700,Hanzac Chen <公顷**** @ gmail.com>在comp.lang.c中写了




除了Walter Roberson正确提到的东西......



我不明白为什么会发生这种情况?
代码1将输出`fff9''
而代码2将输出'1' '
`mod 8'怎么没有效果?

/ *代码1 * /
#include< stdio.h>
#include< ; stdlib.h>

int main(int argc,char * argv [])
{
无符号短a,b,c;
a = 0;
b = 7;

c =(ab)%8;


如果你的平台上的signed int可以保存未签名的所有值,那么'a''和'b''将被提升为减法签名int。

相当于:


unsigned short a,b,c;

signed int sia,同胞;

a = 0;

b = 7;

sia = a;

sib = b;


c =(sia - sib)%8;


括号内的表达式求值为(int)-7。

之一,在C99之前允许的-7%8的两个可能正确值是-7。


当你将(int)-7分配给一个unsigned short,发生超出范围值的无符号

类型的行为。 (int)-7转换为

USHRT_MAX - 7.对于16位无符号短整数,这是0xffff - 7,

等于0xfff9。

printf("%x \ n",c);
返回0;
}

/ *代码2 * /
#include < stdio.h>
#include< stdlib.h>

int main(int argc,char * argv [])
{
unsigned short a ,b,c;
a = 0;
b = 7;

c =(ab)%8U;


同样的事情发生在这里,减法产生的价值为

(int)-7。由于%运算符的另一个操作数具有类型

unsigned int,但是,值(int)-7被提升为(unsigned

int)-7之前执行操作。


printf("%x \ n",c);
返回0;
}




当处理无符号整数类型的促销到更高的

排名整数类型时,这些促销有时会导致更高类型的签名

值。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/ top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu .edu / ~a ... FAQ-acllc.html


Hi,

I don''t understand why this could happen?
The Code 1 will output `fff9''
and the Code 2 will output `1''
How could the `mod 8'' not have effect?

/* Code 1 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8;

printf("%x\n", c);
return 0;
}

/* Code 2 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8U;

printf("%x\n", c);
return 0;
}

解决方案

In article <11**********************@z14g2000cwz.googlegroups .com>,
Hanzac Chen <ha****@gmail.com> wrote:

/* Code 1 */
#include <stdio.h>
#include <stdlib.h>
Note: you do not actually use stdlib.h in any of the code you show.
stdlib.h is, though, the source of EXIT_SUCCESS which you could be
using as your return code instead of using the magic number 0.

int main(int argc, char *argv[])
{
unsigned short a, b, c; The Code 1 will output `fff9''
Not if unsigned short happens to be a different size than on the machine
you happened to test it on.
a = 0;
b = 7;

c = (a-b)%8; printf("%x\n", c);
A printf %x format requires an unsigned int, not an unsigned short.
There''s probably some default argument promotion going on, but
that''s going to affect the result.
return 0;
}


--
If you lie to the compiler, it will get its revenge. -- Eric Sosman


ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:

In article <11**********************@z14g2000cwz.googlegroups .com>,
Hanzac Chen <ha****@gmail.com> wrote:

/* Code 1 */
#include <stdio.h>
#include <stdlib.h>



Note: you do not actually use stdlib.h in any of the code you show.
stdlib.h is, though, the source of EXIT_SUCCESS which you could be
using as your return code instead of using the magic number 0.



But 0 is the least magical of all numbers, and the standard guarantees
that "return 0;" in main() returns a status indicating success.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


On 19 Oct 2005 19:49:04 -0700, "Hanzac Chen" <ha****@gmail.com> wrote
in comp.lang.c:

In addition to the things that Walter Roberson correctly mentioned...

Hi,

I don''t understand why this could happen?
The Code 1 will output `fff9''
and the Code 2 will output `1''
How could the `mod 8'' not have effect?

/* Code 1 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8;
If signed int on your platform can hold all the values of an unsigned
short, ''a'' and ''b'' will be promoted to signed int for the subtraction.
Equivalent to:

unsigned short a, b, c;
signed int sia, sib;
a = 0;
b = 7;
sia = a;
sib = b;

c = (sia - sib)%8;

The expression inside the parentheses evaluates to (int)-7. One of
the two possible correct values for -7 % 8 allowed prior to C99 is -7.

When you assign (int)-7 to an unsigned short, the behavior of unsigned
types with out of range values occurs. (int)-7 is converted to
USHRT_MAX - 7. For a 16-bit unsigned short, this is 0xffff - 7, which
equals 0xfff9.
printf("%x\n", c);
return 0;
}

/* Code 2 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8U;
The same thing happens here, the subtraction yields a value of
(int)-7. Since the other operand of the % operator has the type
unsigned int, however, the value (int)-7 is promoted to (unsigned
int)-7 before the operation is performed.

printf("%x\n", c);
return 0;
}



When dealing with promotions of unsigned integer types to higher
ranking integer types, these promotions sometimes result in signed
values of the higher types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于做`mod'时有符号和无符号的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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