首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问C 移位的有关问题

2012-09-07 
请教C 移位的问题高底位的问题:typedef unsigned shortkal_wcharkal_wchar t1[] {0x1234,0x5678}kal_w

请教C 移位的问题
高底位的问题:
typedef unsigned short kal_wchar;

kal_wchar t1[] = {0x1234,0x5678};
kal_wchar t2[4];

怎样移位得到 
t2[0] = 0x12,
t2[1] = 0x34,
t2[2] = 0x56,
t2[3] = 0x78,



[解决办法]
t2[0] = t1[0] >> 8
t2[1] = t1[0] & 0xff
t2[2] = t1[1] >> 8
t2[3] = t1[1] & 0xff

[解决办法]

C/C++ code
#include <stdio.h>typedef unsigned short kal_wchar;int main(int argc, char *argv[]){    int i, c = 0;    kal_wchar t1[] = {0x1234,0x5678};    kal_wchar t2[4] = {0};    for(i = 0; i < 2; i++)    {        t2[c++] = t1[i] >> 8;        // 取高8位        t2[c++] = t1[i] & 0xFF;        // 取低8位    }    for(i = 0; i < 4; i++)        printf("t2[%d] = %#x\n", i, t2[i]);    return 0;} 

热点排行