bit-fields

How to initialize bitfields with a C++ Constructor?

别来无恙 提交于 2020-07-09 03:25:25
问题 First off, I’m not concerned with portability, and can safely assume that the endianness will not change. Assuming I read a hardware register value, I would like to overlay that register value over bitfields so that I can refer to the individual fields in the register without using bit masks. EDIT: Fixed problems pointed out by GMan, and adjusted the code so it's clearer for future readers. SEE: Anders K. & Michael J's answers below for a more eloquent solution. #include <iostream> /// \class

Default values to bitfield elements

跟風遠走 提交于 2020-07-08 05:24:32
问题 In C++11 one can do struct S {int i = 42;}; and whenever one forgets to initialize the member i, it get default initialized to 42. I Just tried this with bitfields as struct S {int i = 42 : 5;}; and am getting error: expected ';' before ':' token Does this feature exist for bitfield members and if so, how do I use it? 回答1: No, bit-fields do not allow an initializer as part of the member declaration. You can see this in the part of the grammar that describes class members (C++11 and later,

Inconsistent truncation of unsigned bitfield integer expressions between C++ and C in different compilers

徘徊边缘 提交于 2020-04-06 02:26:45
问题 Edit 2 : I was debugging a strange test failure when a function previously residing in a C++ source file but moved into a C file verbatim, started to return incorrect results. The MVE below allows to reproduce the problem with GCC. However, when I, on a whim, compiled the example with Clang (and later with VS), I got a different result! I cannot figure out whether to treat this as a bug in one of the compilers, or as manifestation of undefined result allowed by C or C++ standard. Strangely,

C++ - How to use bitfields

夙愿已清 提交于 2020-03-21 01:50:12
问题 So I recently came across something like this unsigned char ch : 7; inside a struct. I read about it a little. Apparently these are called bit fields. They are used to set the width of data that a char can receive. But how do we use these things. For example, I know that we can set variable ch to be a byte unsigned char ch = 0x61; cout << ch << endl; This will output a However, what do we do with the bitfields? unsigned char ch : 7; ch = 0x61; //This doesn't work for some reason unsigned char

C++ - How to use bitfields

江枫思渺然 提交于 2020-03-21 01:49:14
问题 So I recently came across something like this unsigned char ch : 7; inside a struct. I read about it a little. Apparently these are called bit fields. They are used to set the width of data that a char can receive. But how do we use these things. For example, I know that we can set variable ch to be a byte unsigned char ch = 0x61; cout << ch << endl; This will output a However, what do we do with the bitfields? unsigned char ch : 7; ch = 0x61; //This doesn't work for some reason unsigned char

Multiple inconsistent behavior of signed bit-fields

匆匆过客 提交于 2020-03-18 12:30:28
问题 I have come across a strange behavior on signed bit-fields: #include <stdio.h> struct S { long long a31 : 31; long long a32 : 32; long long a33 : 33; long long : 0; unsigned long long b31 : 31; unsigned long long b32 : 32; unsigned long long b33 : 33; }; long long f31(struct S *p) { return p->a31 + p->b31; } long long f32(struct S *p) { return p->a32 + p->b32; } long long f33(struct S *p) { return p->a33 + p->b33; } int main() { struct S s = { -2, -2, -2, 1, 1, 1 }; long long a32 = -2;

Clarification about Bit-field ordering semantics in C

不羁的心 提交于 2020-01-29 14:42:30
问题 I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10): 6.7.2.1 Structure and union specifiers [...] Semantics [...] An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit