64位转移问题 [英] 64bit shift problem

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

问题描述

为什么这个code不写0作为最后一个元素,但18446744073709551615? (使用g ++编译)

 的#include<的iostream>

使用名字空间std;
诠释的main(){
    无符号长长X =(无符号长长)(-1);
    的for(int i = 0; I< = 64;我++)
    COUT<< I<< << (X GT; I标记)所述;&其中; ENDL;
    COUT<< (X>> 64)<< ENDL;
    返回0;
}
 

解决方案

当您通过更多的比特万字大小移值,它的一般得到由移模字处理尺寸。基本上,它转移了64手段0位等于没有换挡都发生变化。你不应该依赖这个,但因为它不是由标准定义,它可以在不同的体系结构不同。

Why this code does not write 0 as a last element but 18446744073709551615? (compiled with g++)

#include <iostream>

using namespace std;
int main(){
    unsigned long long x = (unsigned long long) (-1);
    for(int i=0; i <= 64; i++)
    	cout << i << " " << (x >> i) << endl;
    cout << (x >> 64) << endl;
    return 0;
}

解决方案

When you shift a value by more bits than word size, it usually gets shifted by mod word-size. Basically, shifting it by 64 means shifting by 0 bits which is equal to no shifting at all. You shouldn't rely on this though as it's not defined by the standard and it can be different on different architectures.

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

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