This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C6000的C编译器,对long变量移位出错

例如:我要将变量tm移位处理?

unsigned long long tm=0x12345678A1B2C3D4
unsigned int xp,tp;

//想得到xp的值为0x345678A1?
 xp = tm>>24;   //为什么的到的数据不是想要的数据

//tp==0x12
tp = tm>>56;  //

  • xp,tp定义的是unsigned int型,xp = tm>>24;这句是把longlong型赋值给int型,就会有问题。

    unsigned int xp,tp;改成unsigned long long xp,tp;

  • 比如变量时是:  unsigned long long tm=0x12345678A1B2C3D4;

     我最终目的要把tm最高字节(0x12)放在字节变量unsigned char 类型的 Index中;

                              把tm剩余变量值(0x345678A1)放在unsigned int类型的fradex中;


    为此,我将你说的xp,tp改成unsigned long long xp,tp;任何混乱。

                               xp = tm>>24;     //单步调试正确

                               fradex = xp;      //单步调试正确


                              xy = tm>>56;   //单步调试,Index变为0;   fradex变为0x34;反而将上面正确的fradex变错误了!

                             Index = yp;       //index仍为0,fradex仍错误!

  • 如果移位一直有问题的话可以尝试一下指针的方法

    long long x = 0x...;

    int *a;

    int b;

    a = &x;

    b = a[n]&0xFF;

    不过用这个方法需要注意大小端的问题