将无符号长long值移位64位 [英] Shifting unsigned long long values by 64 bits

查看:139
本文介绍了将无符号长long值移位64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图将无符号长long值移动64位,这是

我得到的

#include< stdio.h>


int main()

{

unsigned short shiftby = 64;

fprintf(stderr,值(使用硬编码64):%llx \ n,\\ n \\ n \\ n \\ n \\ n \\ n

(((unsigned long long)~0ULL) << 64);

fprintf(stderr,值(没有硬编码64):%llx \ n,\\ n \\ n \\ n \\ n

(( (unsigned long long)~0ULL)<< shiftby));

}


gcc file.c

t2 .c:在函数main中:

t2.c:7:警告:左移计数> =类型宽度

<重要>

价值(使用硬编码64):0

价值(无硬编码64):ffffffffffffffff

< /重要>


如果我们尝试使用

a变量

将值移位64位而不是直接数字64,为什么行为会有所不同?


问候,

Krunal

Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main'':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : ffffffffffffffff
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?

Regards,
Krunal

推荐答案



" krunalb" < kr *********** @ gmail.com写信息

新闻:11 ******************* **@11g2000cwr.googlegrou ps.com ...

"krunalb" <kr***********@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...




我试图转移无符号长多头价值64位,这是

我得到的


#include< stdio.h>


int main()

{

unsigned short shiftby = 64;

fprintf(stderr," Value(using hardcoded 64):%llx \ n",\

(((unsigned long long)~0ULL)<< 64));

fprintf(stderr," Value(w / o硬编码64):%llx \ n",\

(((unsigned long long)~0ULL)<< shiftby));

}


gcc file.c

t2.c:在函数main中:

t2.c:7:警告:左移计数> =类型宽度


<重要>

值(使用硬编码64):0

价值(没有硬编码64):ffffffffffffffff

< / IMPORTANT>


如果我们尝试使用

a变量

将值移位64位,为什么行为会有所不同?反对直接数字64?


问候,

Krunal
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main'':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : ffffffffffffffff
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?

Regards,
Krunal



其未定义的行为。

Its undefined behaviour.


krunalb写道:
krunalb wrote:




我我试图将无符号长long值移动64位,这是

我得到的东西


#include< stdio.h>


int main()

{

unsigned short shiftby = 64;

fprintf(stderr," Value(使用硬编码64):%llx \ n",\

(((unsigned long long)~0ULL)<< 64));

fprintf(stderr,值(没有硬编码64):%llx \ n,\\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n>
(((unsigned long long) )~0ULL)<< shiftby));

}


gcc file.c

t2.c:In函数`main'':

t2.c:7:警告:左移计数> =类型宽度


<重要>

价值(使用硬编码64):0

价值(无硬编码64):ffffffffffffffff

< /重要>


如果我们尝试使用

a变量

将值移位64位而不是直接数字64,为什么行为会有所不同?
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main'':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : ffffffffffffffff
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable
as against direct numeric "64"?



Quoth ISO / IEC 9899:1999:


6.5.7按位移位运算符

[.. 。]

3 [...]如果右操作数的值为负或者

大于或等于提升的宽度

左操作数,行为未定义。

Mark F. Haigh
mf ***** @ sbcglobal.net

Quoth ISO/IEC 9899:1999:

6.5.7 Bitwise Shift Operators
[...]
3 [...] If the value of the right operand is negative or is
greater than or equal to the width of the promoted
left operand, the behavior is undefined.
Mark F. Haigh
mf*****@sbcglobal.net




krunalb写道:

krunalb wrote:




我试图将无符号长long值移位64位,这是

我得到的


#include< stdio.h>


int main()

{

unsigned short shiftby = 64;

fprintf(stderr,Value(使用硬编码64):%llx \ n,\

(((unsigned long long)~0ULL)<< 64));

fprintf(stderr,值(没有硬编码64):%llx \ n,\\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n>
(((unsigned long long) )~0ULL)<< shiftby));

}


gcc file.c

t2.c:In函数`main'':

t2.c:7:警告:左移计数> =类型宽度


<重要>

价值(使用硬编码64):0

价值(无硬编码64):ffffffffffffffff

< /重要>


如果我们尝试使用

a变量将值移位64位而不是直接数字64,为什么行为会有所不同?
Hi,

I am trying to shift unsigned long long value by 64 bits and this is
what i get

#include <stdio.h>

int main()
{
unsigned short shiftby= 64;
fprintf(stderr, "Value (using hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << 64));
fprintf(stderr, "Value (w/o hardcoded 64) : %llx\n", \
(((unsigned long long) ~0ULL) << shiftby));
}

gcc file.c
t2.c: In function `main'':
t2.c:7: warning: left shift count >= width of type
<IMPORTANT>
Value (using hardcoded 64) : 0
Value (w/o hardcoded 64) : ffffffffffffffff
</IMPORTANT>

Why is the behavior different if we try to shift value by 64 bits using
a variable as against direct numeric "64"?



它不在我的AIX系统上,使用xlc编译器...


它是未定义的行为,编译器可以自由地做它喜欢的事情和

编译器不必保持一致......

It isn''t on my AIX system, using the xlc compiler...

It''s undefined behaviour, the compiler is free to do what it likes and
the compiler doesn''t have to be consistent...


这篇关于将无符号长long值移位64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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