bit-fields

How to pack a struct in Visual Studio to 24 bits that contains an uint32_t?

孤街浪徒 提交于 2019-12-10 10:53:07
问题 I am trying to port over an existing application from a 32-Bit ARM-microcontroller to desktop plattforms such as Microsoft Windows. GCC is used on the ARM and I was able successfully compile the application on windows using a 32-bit MinGW-compiler, however I had no success using Microsoft's Visual Studio Compiler and that is the reason why I am asking here for help. Here is what my application is doing: I have some framebuffer consisting of three bytes per pixel, so my memory looks like

Is it safe to use an enum in a bit field?

a 夏天 提交于 2019-12-09 07:37:57
问题 Say, I've got the following struct: typedef struct my_struct{ unsigned long a; unsigned long b; char* c; unsigned int d1 :1; unsigned int d2 :4; unsigned int d3 :4; unsigned int d4 :23; } my_type, *p_type; The field d3 is currently defined by #define s that reach from 0x00 until 0x0D . Actually, d3 is an enumeration. So it's tempting to go ahead and replace unsigned int d3 :4; by my_enum d3 :4; Is this safe/allowed? The code has to compile with various compilers (GCC, Visual Studio, embedded

How slow are bit fields in C++

淺唱寂寞╮ 提交于 2019-12-09 03:29:31
问题 I have a C++ application that includes a number of structures with manually controlled bit fields, something like #define FLAG1 0x0001 #define FLAG2 0x0002 #define FLAG3 0x0004 class MyClass { ' ' unsigned Flags; int IsFlag1Set() { return Flags & FLAG1; } void SetFlag1Set() { Flags |= FLAG1; } void ResetFlag1() { Flags &= 0xffffffff ^ FLAG1; } ' ' }; For obvious reasons I'd like to change this to use bit fields, something like class MyClass { ' ' struct Flags { unsigned Flag1:1; unsigned

Signed bit field in C++14

时光总嘲笑我的痴心妄想 提交于 2019-12-08 19:01:34
问题 I believe that until C++14 a bit field of a struct declared as int was still interpreted as either signed or unsigned , the interpretation being implementation defined. Reference: http://en.cppreference.com/w/cpp/language/bit_field. Is this still the case in C++14? I.e., is the code below guaranteed to work as inteded? #include <iostream> struct X { int f:3; }; int main() { X x; x.f = -2; // is this going to be indeed signed? It seems so. std::cout << x.f << std::endl; // displays -2 } 回答1:

Should bit-fields less than int in size be the subject of integral promotion?

北慕城南 提交于 2019-12-08 16:39:13
问题 Let's say I have following struct : struct A { unsigned int a : 1; unsigned int b : 1; }; What interests me is the type of expression a + b . While technically bit-fields have "type" with size less than int so integral promotion probably should happen and then result is int like it happens to be in gcc and clang. But since it's impossible to extract the exact type of bit-field itself and it will always be deduced to be its "big" type (i.e. unsigned int in this case) is it correct that

Bit fields portability

若如初见. 提交于 2019-12-08 16:25:12
问题 I read here that bit fields are not portable. Does that mean that the code below that defines bit fields (code taken from here) could not compile on certain machines? If so, then why? #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned int widthValidated; unsigned int heightValidated; } status1; /* define a structure with bit fields */ struct { unsigned int widthValidated : 1; unsigned int heightValidated : 1; } status2; int main( ) { printf( "Memory size

gcc suppress warning “too small to hold all values of”

浪尽此生 提交于 2019-12-08 14:42:06
问题 I need to use scoped enums so that I can pass them as specific types to our serialiser. I have given explicit integer values for the enum members of Enum1 . I have put two scoped enums matching the description above into a bitfield thus enum class Enum1 { value1 = 0x0, value2 = 0x1, value3 = 0x2 }; enum class Enum2 { value1 = 0x0, value2, value3, // ... value14 }; struct Example { Enum1 value1 : 2; Enum2 value2 : 6; } Now wherever I use the Example type, I get the warning "'Example::value1'

Why does packing not work across sibling unions or structs

杀马特。学长 韩版系。学妹 提交于 2019-12-08 09:08:33
In the following example I expect the size of complex_t to be the same as uint16_t : 2 bytes, however it's 3 bytes. Removing the second union ("proximity_unsafe") reduces the size to 2 bytes, but I can't figure out the model of the packing rules. #include <stdint.h> #include <stdio.h> typedef union { uint16_t unsafe; struct { uint16_t backwardmotion_unsafe : 1; uint16_t batteryvoltage_unsafe : 1; union { uint16_t dropoff_unsafe : 4; struct { uint16_t dropofffrontleft_unsafe : 1; uint16_t dropofffrontright_unsafe : 1; uint16_t dropoffsideleft_unsafe : 1; uint16_t dropoffsideright_unsafe : 1; }_

How to pass a bitfield (by reference) to a function?

我怕爱的太早我们不能终老 提交于 2019-12-08 08:19:28
My question is how to pass a bitfield instance by reference to a function. I have performed this as shown below, but when i eneter the function DAC_set_gain_code, the processor throws an interupt fault. Is what i am doing correct as far as passing the bitfield goes? I have created a bitfield (see below) which represents a 24bit register on an DAC chip, which i want to write into and lives in the .h file. typedef struct { uint8_t rdwr_u8: 1; uint8_t not_used_u8: 3; uint8_t address_u8: 4; uint8_t reserved_u8: 8; uint8_t data_u8: 8; }GAIN_REG_st; I have a function which initialises the bitfield

operator |= on std::vector<bool>

旧街凉风 提交于 2019-12-08 06:01:15
问题 The following code doesn't compile #include <vector> int main() { std::vector<bool> enable(10); enable[0] |= true; return 0; } giving the error no match for ‘operator|=’ (operand types are ‘std::vector<bool>::reference {aka std::_Bit_reference}’ and ‘bool’) In my real life code I have a bit field with values I want to |= with the result of a function. There are easy way to express the same idea, but is there any good reason for such an operator not to be available ? 回答1: The main reason would