为什么这段代码会导致0?

时间:2010-07-20 03:48:54

标签: c++

我有以下代码

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stdint.h>
using namespace std;
int main(){
    int x;
    cin>>x;
    uint32_t Ex;
    Ex=(x<<1)>>24;
    cout<<Ex<<endl;
    return 0;
}

但它为x的任何值提供0?

我的任务如下:

Computation of the biased exponent Ex of a binary32 datum x.

2 个答案:

答案 0 :(得分:2)

对于'x的任何值',你得到零并没有那么多,但是对于x的任何正值小于0x01000000(16777216)你得零。

这对解释“binary32数据的偏置指数”没有多大帮助。这听起来像是32位浮点(IEEE)数的指数。您可能不得不担心表示的字节顺序等等。

答案 1 :(得分:1)

你得到零是因为你正在向24位移位,这会使你的有效位从末端移开并用左边的零替换它们。

相关问题