位域成員變量詳解
阿新 • • 發佈:2017-09-20
clu 種類 std var 32位 匯總 all str 不能
#include <iostream> #include <string> #include <string.h> #include <stdio.h> #include <malloc.h> #include "list.h" using namespace std; /* 位域成員變量: 該種形式只能出現於結構體或共用體的定義中,是位域定義的標準形式。 其使用方式為 struct name { type var_name : n; }; 含義為,在結構體name匯總,成員變量var_name占用空間為n位。 n為正整數,其值必須小於type類型占用的位數。比如type如果是int, 占4字節32位,那麽n必須是-16~15之間的整數。 註意: 1. type只能為int、unsigne int這2種類型 2. 不能在結構體共用體外定義位域變量*/ typedef struct { int num:4; //指定num所占空間為16位,即2字節 }A; int main(void) { A a; a.num=7; cout << a.num << endl; //7 a.num++; cout << a.num << endl; //註意為: -8 a.num++; cout << a.num << endl; //-7 cout << "------" << endl; a.num=8; //超過-8~7的範圍,則置為8 cout << a.num << endl; //-8 a.num++; cout << a.num << endl; //-7 a.num++; cout << a.num << endl; //-6 }
位域成員變量詳解