user-0 提问: linux下如何取出tcp头部的标志位

回复:
user: user-0
content: 非常感谢两位老师！ 明白了！

user: user-1
content: 再供参考：#include <stdio.h> #include <stdlib.h> char buf[17]; union U {   unsigned short int aa;   struct {     unsigned int bb:7;//(bit 0-6)     unsigned int cc:6;//(bit 7-12)     unsigned int dd:3;//(bit 13-15)   }; } u; void main() {                 //bbbbbbbbbbbbbbbb                 //iiiiiiiiiiiiiiii                 //tttttttttttttttt                 //111111                 //5432109876543210                 //::::::::::::::::     u.aa=0xE07F;//1110000001111111     printf("bb==%d,cc==%d,dd==%d\n",u.bb,u.cc,u.dd);     u.bb=0x41;     u.cc=0x21;//dddccccccbbbbbbb     u.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)

user: user-2
content: 这叫结构体的位字段   struct A { 	unsigned int a : 1; 	unsigned int b : 1; 	unsigned int c : 1; 	unsigned int d : 1; 	unsigned int e : 28; };  int main(int argc, char* argv[]) { 	A a; 	a.a = 1; 	a.b = 1; 	a.c = 1; 	a.d = 1; 	a.e = 0;  	return 0; }  这个结构体只有4字节，低4位分别是1，高28位全为0

user: user-1
content: 仅供参考：#include <stdio.h>  #define WIRTE_UINT(des ,x ,pos ,len)  (                                   \     (des)                                                                 \     &                                                                     \        ~(                                /* 除要修改的位为0，其余都为1 */ \             (                                                             \                 (1<<(len))-1             /* 长度为len位的1             */ \             )                                                             \             <<(32-(pos)-(len))           /* 左移32-(pos)-(len)位       */ \         )                                                                 \     |                                                                     \         (                                                                 \             (x)                                                           \             <<(32-(pos)-(len))                                            \         )                                                                 \ ) #define READ_UINT(src ,pos ,len)      (                                   \     (                                                                     \         (                                                                 \             (unsigned)(src)                                               \         )                                                                 \         >>(32-(pos)-(len))               /* 逻辑右移32-(pos)-(len)位   */ \     )                                                                     \     &                                                                     \     (                                                                     \         (1<<(len))-1                     /* 长度为len位的1             */ \     )                                                                     \ )  int main()                              //                1111111111222222222233 {                                       //bit   01234567890123456789012345678901     unsigned int des = 0x661;           //      00000000000000000000011001100001     unsigned int r;      des = WIRTE_UINT(des ,3 ,28 ,2);    //      00000000000000000000011001101101     printf("des==0x%08X\n",des); 	r =	READ_UINT(des ,28 ,2);     printf("r==%d\n",r);     return 0; } //des==0x0000066D //r==3