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

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

问题描述

为什么这段代码最后一个元素不是写0而是18446744073709551615?(用 g++ 编译)

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;
}

推荐答案

当你移动一个值的位数超过字长时,它通常会被mod word-size移动代码>.基本上,将其移位 64 意味着移位 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天全站免登陆