Skip to content

Commit a333042

Browse files
committed
Add BitField.h
1 parent bbca4ff commit a333042

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

content/various/BitField.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Author:
3+
* License: CC0
4+
* Source: Own work
5+
* Description: Pack many custom size "variables" into one using bitfields.
6+
* WARNING: It is not possible to take references to such "variables".
7+
* Overflow on such "variables" (even an unsigned ones) is implementation defined.
8+
*/
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
struct X {
14+
unsigned a: 10, b: 12, c: 10;
15+
};
16+
17+
static_assert(sizeof(X) == 4);
18+
19+
int main() {
20+
X s;
21+
s.a = 0;
22+
s.b = 10;
23+
s.c = 20;
24+
cout << s.a << " " << s.b << " " << s.c << endl;
25+
}

0 commit comments

Comments
 (0)