zoukankan      html  css  js  c++  java
  • C语言之位域的学习笔记

    直接看程序吧:

    #include <stdio.h>

    struct filter
    {
    unsigned int fl_low:10; //max = 1023
    unsigned int fl_hig:10; //max = 1023
    };

    const struct filter lookup_fl[] = { {0x371,0x2f2},{0x3b1,0x3f5}}; //0xbcb71 0xFD7B1

    int main(void)
    {
    struct filter m1;
    m1.fl_hig = 1024;// 溢出 低 10位为0
    m1.fl_low = 1;
    printf("sizeof(struct filter) = %d. ", sizeof(struct filter));
    printf("m1 = %d. ", m1);
    printf("lookup_fl[0] = 0x%x. ", lookup_fl[0]);//可以看到C语言是把低10位和高10位拼接起来的。
    printf("lookup_fl[0].fl_hig = 0x%x. ", lookup_fl[0].fl_hig);
    printf("lookup_fl[0].fl_low = 0x%x. ", lookup_fl[0].fl_low);
    printf("lookup_fl[1] = 0x%x. ", lookup_fl[1]);//可以看到C语言是把低10位和高10位拼接起来的,在内存中是连续的
    printf("lookup_fl[1].fl_hig = 0x%x. ", lookup_fl[1].fl_hig);
    printf("lookup_fl[1].fl_low = 0x%x. ", lookup_fl[1].fl_low);
    return 0;
    }

    运行结果:

    简单解释,这里我定义了2个10bit的变量作为一个结构体数据类型,分别打印了在内存中占用的字节数,4字节(32位系统),以及低字节和高字节,以及溢出的时候,

    值是多少,从上门我们可以看到C语言是把低10位和高10位拼接起来的,在内存中是连续的。

    意义:以后可以用位域结合位运算符来执行相关的位操作了。

        欢迎和我讨论,转贴请注明出处,谢谢。

          -------------------cofin_add

  • 相关阅读:
    M1卡的工作原理【转】
    磁卡ID卡IC卡的区别【转】
    磁卡结构【转】
    M1卡破解(自从学校升级系统之后,还准备在研究下)【转】
    RunJS演示代码
    Linux中inet_aton的问题(IP转整数)
    使用Hive UDF和GeoIP库为Hive加入IP识别功能
    hive下UDF函数的使用
    linux下smb
    使用Spring MVC表单标(转)
  • 原文地址:https://www.cnblogs.com/cofin/p/6812100.html
Copyright © 2011-2022 走看看