-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path009_Bitfields.cpp
More file actions
31 lines (26 loc) · 868 Bytes
/
009_Bitfields.cpp
File metadata and controls
31 lines (26 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// bitfields.cpp by Bill Weinman <http://bw.org/>
#include <cstdio>
using namespace std;
struct preferences {
bool likesMusic : 1;
bool hasHair : 1;
bool hasInternet : 1;
bool hasDinosaur : 1;
unsigned int numberOfChildren : 4;
};
int main() {
struct preferences homer;
homer.likesMusic = true;
homer.hasHair = false;
homer.hasInternet = true;
homer.hasDinosaur = false;
homer.numberOfChildren = 3;
printf("sizeof int: %ld bits\n", sizeof(int) * 8);
printf("sizeof homer: %ld bits\n", sizeof(homer) * 8);
if(homer.likesMusic) printf("homer likes music\n");
if(homer.hasHair) printf("homer has hair\n");
if(homer.hasInternet) printf("homer has net\n");
if(homer.hasDinosaur) printf("homer has a dino\n");
printf("homer has %d children\n", homer.numberOfChildren);
return 0;
}