首页 > 用户发贴区 > 编程问题提问区 > 移位时的小问题,请指教
2008
05-01

移位时的小问题,请指教

#include <stdlib.h>
#include <stdio.h>
#include <time.h>


typedef time_t TIME;


void main(void)
{
 int  i = 0;
 unsigned char a[3];
 long b[3];
 TIME t,tf;
 t = time(NULL);


 a[0] = (unsigned char)(t);
 a[1] = (unsigned char)(t>>8);
 a[2] = (unsigned char)(t>>16);
 a[3] = (unsigned char)(t>>24);
 printf(“%x, %x, %x, %x\n”, a[0], a[1], a[2], a[3]);


 b[0] = (long)a[0];
 b[1] = (long)(a[1]<<8);
 b[2] = (long)(a[2]<<16);
 b[3] = (long)(a[3]<<24);
 tf   = b[0] + b[1] + b[2] + b[3];
 printf(“%x, %x, %x, %x\n”, b[0], b[1], b[2], b[3]);
 printf(“%x\n”, tf);
}


也就是将一个time_t格式的数据转化为4个byte型的,但是在执行这个语句 


b[1] = (long)(a[1]<<8);时,a[1]还是byte的数据,左移八位不是变成全零了麽。


但是这个程序最后是能够正错恢复出来tf的,也就是tf = t,这是为什么啊???


 


移位时的小问题,请指教》有 1 条评论

  1. yushijun 说:

    b[0] = (long)a[0];
     b[1] = (long)a[1]<<8;
     b[2] = (long)a[2]<<16;
     b[3] = (long)a[3]<<24;
     tf   = b[0] + b[1] + b[2] + b[3];

    和b[0] = (long)a[0];
     b[1] = (long)(a[1]<<8);
     b[2] = (long)(a[2]<<16);
     b[3] = (long)(a[3]<<24);
    左移产生的效果一样

    估计产生了一个临时变量把

留下一个回复