【记录】C++中的位域
代码如下:
// 临界区.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) { struct bs { unsigned a : 1; unsigned b : 3; unsigned c : 32; }bit,*pbit; bit.a = 1; bit.b = 7; bit.c = 15; //printf("%d,%d,%d", bit.a, bit.b, bit.c); pbit = &bit; pbit->a = 0; pbit->b &= 3; pbit->c |= 1; printf("%d,%d,%d", pbit->a, pbit->b, pbit->c); getchar(); return 0; }
位域主要的位数的定义,最大可到255,定义到32位。
看看反汇编就都明白了。
00053CA0 55 push ebp 00053CA1 8B EC mov ebp,esp 00053CA3 81 EC E0 00 00 00 sub esp,0E0h 00053CA9 53 push ebx 00053CAA 56 push esi 00053CAB 57 push edi 00053CAC 8D BD 20 FF FF FF lea edi,[ebp-0E0h] 00053CB2 B9 38 00 00 00 mov ecx,38h 00053CB7 B8 CC CC CC CC mov eax,0CCCCCCCCh 00053CBC F3 AB rep stos dword ptr es:[edi] 00053CBE A1 24 80 05 00 mov eax,dword ptr ds:[00058024h] 00053CC3 33 C5 xor eax,ebp 00053CC5 89 45 FC mov dword ptr [ebp-4],eax 9: struct bs 10: { 11: unsigned a : 1; 12: unsigned b : 3; 13: unsigned c : 32; 14: }bit,*pbit; 15: bit.a = 1; 00053CC8 8B 45 F0 mov eax,dword ptr [bit] 00053CCB 83 C8 01 or eax,1 00053CCE 89 45 F0 mov dword ptr [bit],eax 16: bit.b = 7; 00053CD1 8B 45 F0 mov eax,dword ptr [bit] 00053CD4 83 C8 0E or eax,0Eh 00053CD7 89 45 F0 mov dword ptr [bit],eax 17: bit.c = 15; 00053CDA 8B 45 F4 mov eax,dword ptr [ebp-0Ch] 00053CDD 83 E0 00 and eax,0 00053CE0 83 C8 0F or eax,0Fh 00053CE3 89 45 F4 mov dword ptr [ebp-0Ch],eax 18: //printf("%d,%d,%d", bit.a, bit.b, bit.c); 19: pbit = &bit; 00053CE6 8D 45 F0 lea eax,[bit] 00053CE9 89 45 E4 mov dword ptr [pbit],eax 20: pbit->a = 0; 00053CEC 8B 45 E4 mov eax,dword ptr [pbit] 00053CEF 8B 08 mov ecx,dword ptr [eax] 00053CF1 83 E1 FE and ecx,0FFFFFFFEh 00053CF4 8B 55 E4 mov edx,dword ptr [pbit] 00053CF7 89 0A mov dword ptr [edx],ecx 21: pbit->b &= 3; 00053CF9 8B 45 E4 mov eax,dword ptr [pbit] 00053CFC 8B 08 mov ecx,dword ptr [eax] 00053CFE D1 E9 shr ecx,1 00053D00 83 E1 07 and ecx,7 00053D03 83 E1 03 and ecx,3 00053D06 83 E1 07 and ecx,7 00053D09 D1 E1 shl ecx,1 00053D0B 8B 55 E4 mov edx,dword ptr [pbit] 00053D0E 8B 02 mov eax,dword ptr [edx] 00053D10 83 E0 F1 and eax,0FFFFFFF1h 00053D13 0B C1 or eax,ecx 00053D15 8B 4D E4 mov ecx,dword ptr [pbit] 00053D18 89 01 mov dword ptr [ecx],eax 22: pbit->c |= 1; 00053D1A 8B 45 E4 mov eax,dword ptr [pbit] 00053D1D 8B 48 04 mov ecx,dword ptr [eax+4] 00053D20 83 C9 01 or ecx,1 00053D23 8B 55 E4 mov edx,dword ptr [pbit] 00053D26 8B 42 04 mov eax,dword ptr [edx+4] 00053D29 83 E0 00 and eax,0 00053D2C 0B C1 or eax,ecx 00053D2E 8B 4D E4 mov ecx,dword ptr [pbit] 00053D31 89 41 04 mov dword ptr [ecx+4],eax 23: printf("%d,%d,%d", pbit->a, pbit->b, pbit->c); 00053D34 8B 45 E4 mov eax,dword ptr [pbit] 00053D37 8B 48 04 mov ecx,dword ptr [eax+4] 00053D3A 8B F4 mov esi,esp 00053D3C 51 push ecx 00053D3D 8B 55 E4 mov edx,dword ptr [pbit] 00053D40 8B 02 mov eax,dword ptr [edx] 00053D42 D1 E8 shr eax,1 00053D44 83 E0 07 and eax,7 00053D47 50 push eax 00053D48 8B 4D E4 mov ecx,dword ptr [pbit] 00053D4B 8B 11 mov edx,dword ptr [ecx] 00053D4D 83 E2 01 and edx,1 00053D50 52 push edx 00053D51 68 C0 58 05 00 push 558C0h 00053D56 FF 15 24 91 05 00 call dword ptr ds:[59124h] 00053D5C 83 C4 10 add esp,10h 00053D5F 3B F4 cmp esi,esp 00053D61 E8 DA D3 FF FF call __RTC_CheckEsp (051140h)
在反汇编中我们可以看到当申明struct结构体的时候里面的所有变量操作并不占用空间。
当对结构体成员等变量操作的时候才进行占用空间。
同时所有定义的十进制在结构体中都会转换成二进制进行位域比较。
CKing blck