bitwise opertion

来源:互联网 发布:淘宝保证金交30的条件 编辑:程序博客网 时间:2024/06/05 12:49



#include<stdio.h>#include<stdlib.h>#include<stdint.h>typedef struct{  uint8_t a:6;  uint8_t b:4;  uint8_t day:5;  uint8_t hour:5;}my_t;int main(){my_t *p = (my_t *)malloc(sizeof(my_t));  if(!p)   {  return -1;}p->a = 17;p->b = 13;p->day = 31;p->hour = 23;uint8_t *buf = calloc(3, sizeof(uint8_t));if(!buf){free(p);return -2;}  printf("Hello size=%d, %d, %d, %d, %d\n", sizeof(my_t), p->a, p->b, p->day, p->hour);    buf[0] = (p->b<<6)|(p->a & 0x3f);  buf[1] = (p->hour<<7)|(p->day<<2)|(p->b>>2);  buf[2] = (p->hour>>1)&0x0f;  printf("buf[0]=0x%02x, buf[1]=0x%02x, buf[2]=0x%02x\n", buf[0], buf[1], buf[2]);if(p)free(p);if(buf)free(buf);  return 0;}


0 0
原创粉丝点击