• 主页
• 问答

# 位域与字节序同时转化的问题

suncs2001 2013-03-06 12:51:15

``````
struct A
{
short data;
short b:6;
short c:3;
short d:7;
}
``````

``````
struct A
{
short data;
#if BIG_ENDIAN
short b:6;
short c:3;
short d:7;
#else
short d:7;
short c:3;
short b:6;
#endif
}
``````

``````

void rx_func(void*data)
{
A* a;
a = (A*)data;
a->b = 0;
a->c = 0;
a->d = 0;

}

``````
...全文
287 点赞 收藏 14

14 条回复

ruanben 2014-07-03

mujiok2003 2013-03-07

mujiok2003 2013-03-07

nice_cxf 2013-03-06

ForestDB 2013-03-06

mymtom 2013-03-06

sinservice 2013-03-06

nice_cxf 2013-03-06

``````struct A
{
short data;
#if BIG_ENDIAN
short b:6;
short c:3;
short d:7;
#else
short d:7;
short c:3;
short b:6;
#endif
}``````

``````#include <stdio.h>
#include <stdlib.h>
char buf[17];
union U {
unsigned short int aa;
struct S {
unsigned int bb:7;//(bit 0-6)
unsigned int cc:6;//(bit 7-12)
unsigned int dd:3;//(bit 13-15)
} s;
} u;
void main() {
//bbbbbbbbbbbbbbbb
//iiiiiiiiiiiiiiii
//tttttttttttttttt
//111111
//5432109876543210
//::::::::::::::::
u.aa=0xE07F;//1110000001111111
printf("bb==%d,cc==%d,dd==%d\n",u.s.bb,u.s.cc,u.s.dd);
u.s.bb=0x41;
u.s.cc=0x21;//dddccccccbbbbbbb
u.s.dd=5;   //1011000011000001
printf("aa==0x%04X==%016s(2)\n",u.aa,itoa(u.aa,buf,2));
}
//bb==127,cc==0,dd==7
//aa==0xB0C1==1011000011000001(2)
``````

AnYidan 2013-03-06

suncs2001 2013-03-06

suncs2001 2013-03-06

sinservice 2013-03-06

C语言

6.3w+

C语言相关问题讨论