結構中的位字段

與C語言一樣,C++也允許指定佔用特定位數的結構成員,這使得創建與某個硬件設備上的寄存器對應的數據結構非常方便。字段的類型應爲整形或枚舉,接下來是冒號,冒號後面是一個數字,它指定了使用的位數。可以使用沒有名稱的字段來提供間距。每個成員都被稱爲位字段。下面是一個例子:

struct torgle_register

{

unsigned int SN : 4; // 4bit for SN value

unsigned int : 4; // 4bit unused

bool goodIn : 1; // valid input (1 bit)

bool goodTorgle : 1; // successful torgling

};

可以像通常那樣初始化這些字段,還可以使用標準的結構表示法來訪問位字段:

torgle_register tr = {14, true, false};


代碼:

#include <stdio.h>
#include <iostream>

#pragma pack(1)

struct test_register1
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
};

struct test_register2
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	unsigned int SM8 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
};

struct test_register3
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	unsigned int SM8 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
	bool SN8 : 1;
};

int main()
{
	int size1 = sizeof(test_register1);
	int size2 = sizeof(test_register2);
	int size3 = sizeof(test_register3);
}
size1 = 5;

size2 = 9;

size3 = 10;


#include <stdio.h>
#include <iostream>

#pragma pack(4)

struct test_register1
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
};

struct test_register2
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	unsigned int SM8 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
};

struct test_register3
{
	unsigned int SM0 : 4;
	unsigned int SM1 : 4;
	unsigned int SM2 : 4;
	unsigned int SM3 : 4;
	unsigned int SM4 : 4;
	unsigned int SM5 : 4;
	unsigned int SM6 : 4;
	unsigned int SM7 : 4;
	unsigned int SM8 : 4;
	bool SN0 : 1;
	bool SN1 : 1;
	bool SN2 : 1;
	bool SN3 : 1;
	bool SN4 : 1;
	bool SN5 : 1;
	bool SN6 : 1;
	bool SN7 : 1;
	bool SN8 : 1;
};

int main()
{
	int size1 = sizeof(test_register1);
	int size2 = sizeof(test_register2);
	int size3 = sizeof(test_register3);
}
size1=8

size2=12

size3=12


字節對齊不一樣的情況下結構體的大小也不一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章