关于转移 [英] about shifting

查看:60
本文介绍了关于转移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道左右移动正常,但我不知道如果

它是负的会发生什么。

例如

int x = -2;

x<< = 1; //这里发生了什么

i know left and right shift normally,but i cant know what happens if
it is negative.
for example
int x=-2;
x<<=1;//what happens here

推荐答案

lak写道:
lak wrote:

i知道左右移位正常,但我不知道如果

它是负的会发生什么。

例如

int x = -2;

x<< = 1; //这里发生什么
i know left and right shift normally,but i cant know what happens if
it is negative.
for example
int x=-2;
x<<=1;//what happens here



与平常没什么不同...... -2的位表示被移动

向左一位......

nothing different from usual.. the bit representation of -2 is shifted
one bit to the left...


cat test_shift.c
cat test_shift.c



#include< stdio.h>

int main(void)

{


int k = -1;

printf(" k is%d(%x)\ n",k,k);

k<< = 4;

printf(" k is%d(%x)\ n",k,k);


返回(0);

}

#include <stdio.h>
int main(void)
{

int k = -1;
printf("k is %d (%x)\n", k, k);
k<<=4;
printf("k is %d (%x)\n", k, k);

return (0);
}


gcc -Wall -o test_shift test_shift.c& ;&安培; ./test_shift
gcc -Wall -o test_shift test_shift.c && ./test_shift



k是-1(ffffffff)

k是-16(fffffff0)


Pietro Cerutti

k is -1 (ffffffff)
k is -16 (fffffff0)

Pietro Cerutti


lak< la ******** @ gmail.comwrote:
lak <la********@gmail.comwrote:

i知道左右移动正常,但我不知道如果

它是负的会发生什么。

例如

int x = -2 ;

x<< = 1; //这里发生了什么
i know left and right shift normally,but i cant know what happens if
it is negative.
for example
int x=-2;
x<<=1;//what happens here



这是正确的:你不知道。


来自ISO C标准的第6.5.7段:


#4 E1的结果<< E2是E1左移E2位位置;空出

#位用零填充。如果E1具有无符号类型,则值为

#,结果为E1 x 2 E2,比结果类型中可表示的最大值
#非负值,并且E1 x 2 E2可以在结果中表示

#type,那么这就是结果值;否则,行为是

#undefined。 ^^^^^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^

注意下^^^内衬位。因为在你的情况下x既不是无符号的

整数,也不是具有正值的有符号整数,所以你的代码的行为是未定义的;这就意味着,就ISO C而言,你无法知道会发生什么。 (有可能发现使用具有特定编译设置的特定编译器在特定计算机上发生的事情,但我建议反对它;在下一个

系统,甚至在下一级优化,结果都可以轻松地与b $ b不同。)


Richard

That is correct: you cannot know that.

From paragraph 6.5.7 in the ISO C Standard:

# 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
# bits are filled with zeros. If E1 has an unsigned type, the value of
# the result is E1 x 2 E2 ,reduced modulo one more than the maximum
# value representable in the result type. If E1 has a signed type and
# nonnegative value, and E1 x 2 E2 is representable in the result
# type, then that is the resulting value; otherwise, the behavior is
# undefined. ^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^

Note the under^^^lined bit. Since in your case x is neither an unsigned
integer, nor a signed integer with a positive value, the behaviour of
your code is undefined; and this means that, as far as ISO C is
concerned, you cannot know what happens. (It may be possible to discover
what happens on a particular computer using a particular compiler with
particular compilation settings, but I advise against it; on the next
system, or even on the next level of optimisation, the result can easily
be different.)

Richard


" Richard Bos" <rl*@hoekstra-uitgeverij.nlaécritdansle message de news:
46 ***************** @ news.xs4all.nl ...
"Richard Bos" <rl*@hoekstra-uitgeverij.nla écrit dans le message de news:
46*****************@news.xs4all.nl...

lak< la ******** @ gmail.comwrote:
lak <la********@gmail.comwrote:

>我知道左右移动正常,但我不知道会发生什么如果
它是否定的。
例如
int x = -2;
x<< = 1; //这里发生了什么
>i know left and right shift normally,but i cant know what happens if
it is negative.
for example
int x=-2;
x<<=1;//what happens here



这是正确的:你不知道。


来自ISO C标准的第6.5.7段:


#4 E1的结果<< E2是E1左移E2位位置;空出

#位用零填充。如果E1具有无符号类型,则值为

#,结果为E1 x 2 E2,比结果类型中可表示的最大值
#非负值,并且E1 x 2 E2可以在结果中表示

#type,那么这就是结果值;否则,行为是

#undefined。 ^^^^^^^^^^^^^^^^^^^^^^^^^^

^^^^^^^^^

注意下^^^内衬位。因为在你的情况下x既不是无符号的

整数,也不是具有正值的有符号整数,所以你的代码的行为是未定义的;这就意味着,就ISO C而言,你无法知道会发生什么。 (有可能发现使用具有特定编译设置的特定编译器在特定计算机上发生的事情,但我建议反对它;在下一个

系统,或者甚至在下一级优化,结果很容易与b $ b不同。)


That is correct: you cannot know that.

From paragraph 6.5.7 in the ISO C Standard:

# 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
# bits are filled with zeros. If E1 has an unsigned type, the value of
# the result is E1 x 2 E2 ,reduced modulo one more than the maximum
# value representable in the result type. If E1 has a signed type and
# nonnegative value, and E1 x 2 E2 is representable in the result
# type, then that is the resulting value; otherwise, the behavior is
# undefined. ^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^

Note the under^^^lined bit. Since in your case x is neither an unsigned
integer, nor a signed integer with a positive value, the behaviour of
your code is undefined; and this means that, as far as ISO C is
concerned, you cannot know what happens. (It may be possible to discover
what happens on a particular computer using a particular compiler with
particular compilation settings, but I advise against it; on the next
system, or even on the next level of optimisation, the result can easily
be different.)



Richard是正确。

但是,如果你期望x <<<< = 1等于x + = x,那么

"正常"对于普通的2s补充机器,你可以这样做

写下后者。


-

Chqrlie。

Richard is correct.
However, if you expected x <<= 1 to be equivalent to x += x, as would
"normally" be the case on regular 2s-complement machines, you might as well
write the latter.

--
Chqrlie.


这篇关于关于转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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