C语言位域使用


 

  信息的存取一般以字节为单位。实际上,有时存储一个信息不必用一个或多个字节,例如,“真”或“假”用0或1表示,只需1位即可。

   在计算机用于过程控制、参数检测或数据通信领域时,控制信息往往只占一个字节中的一个或几个二进制位,常常在一个字节中放几个信息


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
struct usb
{
    int sample_rate;
    int sample_bits;
    unsigned int connect:1;  //int 会出现问题  -1  0xffffffff
};
//0-8 ?
int main()
{
    struct usb usb_device;
    usb_device.connect = 1;
    printf("usb device is length is %d ",sizeof(usb_device.sample_bits));
    //printf("usb device is length is %d ",sizeof(usb_device.connect));
    printf("usb device is %d ",usb_device.connect);
    //printf("usb undef is %d ",usb_device.undef);

    for(;;);
    return 0;
}
 

一勤天下无难事。
原文地址:https://www.cnblogs.com/nowroot/p/13765423.html