1 | typedef unsigned int uint32_t; |
2 | |
3 | enum MaskingOperator { |
4 | eMaskingOperatorDefault = 0, |
5 | eMaskingOperatorAnd = 1, |
6 | eMaskingOperatorOr = 2, |
7 | eMaskingOperatorXor = 3, |
8 | eMaskingOperatorNand = 4, |
9 | eMaskingOperatorNor = 5 |
10 | }; |
11 | |
12 | class MaskedData { |
13 | private: |
14 | uint32_t value; |
15 | uint32_t mask; |
16 | MaskingOperator oper; |
17 | |
18 | public: |
19 | MaskedData(uint32_t V = 0, uint32_t M = 0, |
20 | MaskingOperator P = eMaskingOperatorDefault) |
21 | : value(V), mask(M), oper(P) {} |
22 | |
23 | uint32_t apply() { |
24 | switch (oper) { |
25 | case eMaskingOperatorAnd: |
26 | return value & mask; |
27 | case eMaskingOperatorOr: |
28 | return value | mask; |
29 | case eMaskingOperatorXor: |
30 | return value ^ mask; |
31 | case eMaskingOperatorNand: |
32 | return ~(value & mask); |
33 | case eMaskingOperatorNor: |
34 | return ~(value | mask); |
35 | case eMaskingOperatorDefault: // fall through |
36 | default: |
37 | return value; |
38 | } |
39 | } |
40 | |
41 | void setValue(uint32_t V) { value = V; } |
42 | |
43 | void setMask(uint32_t M) { mask = M; } |
44 | |
45 | void setOperator(MaskingOperator P) { oper = P; } |
46 | }; |
47 | |
48 | int main() { |
49 | MaskedData data_1(0xFF0F, 0xA01F, eMaskingOperatorAnd); |
50 | MaskedData data_2(data_1.apply(), 0x1AFC, eMaskingOperatorXor); |
51 | MaskedData data_3(data_2.apply(), 0xFFCF, eMaskingOperatorOr); |
52 | MaskedData data_4(data_3.apply(), 0xAABC, eMaskingOperatorAnd); |
53 | MaskedData data_5(data_4.apply(), 0xFFAC, eMaskingOperatorNor); |
54 | MaskedData data_6(data_5.apply(), 0x0000BEEF, eMaskingOperatorAnd); |
55 | return data_6.apply(); // <-- what comes out of here? |
56 | } |
57 | |