bit-fields

When is it worthwhile to use bit fields?

℡╲_俬逩灬. 提交于 2019-12-28 05:35:08
问题 Is it worthwhile using C's bit-field implementation? If so, when is it ever used? I was looking through some emulator code and it looks like the registers for the chips are not being implemented using bit fields. Is this something that is avoided for performance reasons (or some other reason)? Are there still times when bit-fields are used? (ie firmware to put on actual chips, etc) 回答1: Bit-fields are typically only used when there's a need to map structure fields to specific bit slices,

Variable-sized bitfields with aliasing

对着背影说爱祢 提交于 2019-12-24 16:54:19
问题 I have some struct containig a bitfield, which may vary in size. Example: struct BitfieldSmallBase { uint8_t a:2; uint8_t b:3; .... } struct BitfieldLargeBase { uint8_t a:4; uint8_t b:5; .... } and a union to access all bits at once: template<typename T> union Bitfield { T bits; uint8_t all; // <------------- Here is the problem bool operator & (Bitfield<T> x) const { return !!(all & x.all); } Bitfield<T> operator + (Bitfield<T> x) const { Bitfield<T> temp; temp.all = all + x.all; //works,

Aggregate bitfield values with binary OR

扶醉桌前 提交于 2019-12-24 11:28:40
问题 I have a table with int values being used as bitfields (where each bit is a flag). Now I would like to aggregate them with a binary operation (in my case OR) so that: SELECT 1 AS bitfield INTO #TABLE UNION ALL SELECT 1 + 2 + 8 + 32 UNION ALL SELECT 2 + 128 UNION ALL SELECT 2 + 32 SELECT AND_AGGR(bitfield) -- Invalid because AND_AGGR doesn't exist FROM #TABLE DROP #TABLE would result in the value 171 What would be a good way to do this that hopefully doesn't require a lot of | and MAX (but if

Packing bools with bit field (C++)

隐身守侯 提交于 2019-12-23 13:02:55
问题 I'm trying to interface with Ada code using C++, so I'm defining a struct using bit fields, so that all the data is in the same place in both languages. The following is not precisely what I'm doing, but outlines the problem. The following is also a console application in VS2008, but that's not super relevant. using namespace System; int main() { int array1[2] = {0, 0}; int *array2 = new int[2](); array2[0] = 0; array2[1] = 0; #pragma pack(1) struct testStruct { // Word 0 (desired) unsigned a

Why output of the below code is -1 and -2? [duplicate]

怎甘沉沦 提交于 2019-12-23 05:26:43
问题 This question already has answers here : Struct variable doesn't changed by assignment (3 answers) Closed 2 years ago . Why output of the below code is -1 and -2, It should be 1 and 2, Right? Also on a 64 bit server size of the below structure is 4 Bytes, It should be 8 Bytes right? #include<stdio.h> struct st { int a:1; int b:2; }; main() { struct st obj={1,2}; printf("a = %d\nb = %d\n",obj.a,obj.b); printf("Size of struct = %d\n",sizeof(obj)); } 回答1: Compile with all the warnings enabled,

Why does packing not work across sibling unions or structs

自闭症网瘾萝莉.ら 提交于 2019-12-23 03:03:44
问题 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

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

扶醉桌前 提交于 2019-12-23 02:45:38
问题 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

Typedef a bitfield variable

核能气质少年 提交于 2019-12-22 11:27:40
问题 I want to have a typedef that is 1-bit integer, so I though of this typedef int:1 FLAG; but I'm getting errors with it, is there a way I can do so? Thanks 回答1: No. The smallest addressable "thing" in a C Program is a byte or char . A char is at least 8 bits long. So you cannot have a type (or objects of any type) with less than 8 bits. What you can do is have a type for which objects occupy at least as many bits as a char and ignore most of the bits #include <limits.h> #include <stdio.h>

Size of packed struct with union of bit fields less than 8 bits in C

筅森魡賤 提交于 2019-12-22 08:42:18
问题 Is it possible in C to get the size of the following structure to be 2? #include <stdio.h> struct union_struct { char foo; char bar : 2; union { char foobar1 : 6; char foobar2 : 6; }; }; int main(void) { printf("size of union struct: %d\n", sizeof(struct union_struct)); return 0; } output, compiled with gcc: size of union struct: 3 回答1: If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently: #ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED

Why class size increases when int64_t changes to int32_t

♀尐吖头ヾ 提交于 2019-12-21 03:12:27
问题 In my first example I have two bitfields using int64_t . When I compile and get the size of the class I get 8. class Test { int64_t first : 40; int64_t second : 24; }; int main() { std::cout << sizeof(Test); // 8 } But when I change the second bitfeild to be a int32_t the size of the class doubles to 16: class Test { int64_t first : 40; int32_t second : 24; }; int main() { std::cout << sizeof(Test); // 16 } This happens on both GCC 5.3.0 and MSVC 2015. But why? 回答1: In your first example