zoukankan      html  css  js  c++  java
  • 位域

    注意空域。unsigned默认是4个字节。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 struct B1{
     5     unsigned a:4;
     6     unsigned b:4;
     7 };
     8 
     9 struct B2{
    10     unsigned a:4;
    11     unsigned :0;
    12     unsigned b:4;
    13 };
    14 
    15 struct B3{
    16     unsigned char a:4;
    17     unsigned char b:4;
    18 };
    19 
    20 struct B4{
    21     unsigned char a:4;
    22     unsigned :0;
    23     unsigned char b:4;
    24 };
    25 
    26 struct B5{
    27     unsigned char a:4;
    28     unsigned char b:4;
    29     unsigned :0;
    30 };
    31 
    32 struct B6{
    33     unsigned char a:4;
    34     unsigned char b:5;
    35 };
    36 int main(int argc, char** argv) {
    37     cout << "sizeof(B1): " << sizeof(B1) << endl;
    38     cout << "sizeof(B2): " << sizeof(B2) << endl;
    39     cout << "sizeof(B3): " << sizeof(B3) << endl;
    40     cout << "sizeof(B4): " << sizeof(B4) << endl;
    41     cout << "sizeof(B5): " << sizeof(B5) << endl;
    42     cout << "sizeof(B6): " << sizeof(B6) << endl;
    43     return 0;
    44 }

    输出:

    1 root@xxj-VirtualBox:~/interview# ./bitfield 
    2 sizeof(B1): 4
    3 sizeof(B2): 8
    4 sizeof(B3): 1
    5 sizeof(B4): 5
    6 sizeof(B5): 4
    7 sizeof(B6): 2
  • 相关阅读:
    poj 3026 Borg Maze
    poj2828 Buy Tickets
    poj3264 Balanced Lineup
    高精度运算
    poj1035 Spell checker
    poj2318 TOYS 点积叉积理解
    求两直线交点的一般做法
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业07
  • 原文地址:https://www.cnblogs.com/linyx/p/4003360.html
Copyright © 2011-2022 走看看