bit-fields

How to enforce the struct bit order with the GCC compiler?

坚强是说给别人听的谎言 提交于 2019-11-30 12:43:53
I was wondering if there is a GCC C Compiler directive that allows me to determine the bit order for packing of a structure? Something to the likes of: #pragma bit_order left The rationale for such a need is that I have the following structure: struct { union { unsigned char BYTE; struct { unsigned char B0: 1; unsigned char B1: 1; unsigned char B2: 1; unsigned char B3: 1; unsigned char B4: 4; }BIT; }ITEM; } myStruct; With this structure, I would like the compiler to pack it this way: Bit order: | 7 6 5 4 3 2 1 0 | Label: |B0 B1 B2 B3 B4 B5 B6 B7 | Rather than how GCC does it: Bit order: | 7 6

How to use binary flags in Core Data?

人盡茶涼 提交于 2019-11-30 11:43:11
问题 I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001" ? I'm also wondering if this is possible with a binary typed attribute ? 回答1: NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL

How to assign value to a struct with bit-fields?

大城市里の小女人 提交于 2019-11-30 09:18:54
I have a struct with bit-fields (totally 32 bit width) and I have a 32-bit variable. When I try to assign the variable value to my struct, I got an error: error: conversion from ‘uint32_t {aka unsigned int}’ to non-scalar type ‘main()::CPUID’ requested. struct CPUIDregs { uint32_t EAXBuf; }; CPUIDregs CPUIDregsoutput; int main () { struct CPUID { uint32_t Stepping : 4; uint32_t Model : 4; uint32_t FamilyID : 4; uint32_t Type : 2; uint32_t Reserved1 : 2; uint32_t ExtendedModel : 4; uint32_t ExtendedFamilyID : 8; uint32_t Reserved2 : 4; }; CPUID CPUIDoutput = CPUIDregsoutput.EAXBuf; Do you have

c union and bitfields

送分小仙女□ 提交于 2019-11-30 08:32:15
问题 Can bitfields be used in union? 回答1: Yes, they can be. Why not? Bit-fields in unions behave in the same way they behave anywhere else. There's nothing special about bit-fields in unions (or unions with bit-fields). 回答2: Yes it is possible, but I would recommend against it. The length and packing of bitfields is not portable. The size of the union will be difficult to predict (see here). There is a certain amount of complexity that you introduce into the code when you use unions or bitfields.

Efficient bit-fiddling in a LFSR implementation

我是研究僧i 提交于 2019-11-30 05:17:36
问题 Although I have a good LSFR C implementation I thought I'd try the same in Haskell - just to see how it goes. What I came up with, so far, is two orders of magnitude slower than the C implementation, which begs the question: How can the performance be improved? Clearly, the bit-fiddling operations are the bottleneck, and the profiler confirms this. Here's the baseline Haskell code using lists and Data.Bits : import Control.Monad (when) import Data.Bits (Bits, shift, testBit, xor, (.&.), (.|.)

Why are non-const references to bitfields prohibited?

家住魔仙堡 提交于 2019-11-30 02:46:07
问题 Section 9.6/3 in C++11 is unusually clear: "A non-const reference shall not be bound to a bit-field." What is the motivation behind this prohibition? I understand that it's not possible to directly bind a reference to a bitfield. But if I declare something like this, struct IPv4Header { std::uint32_t version:4, // assumes the IPv4 Wikipedia entry is correct IHL:4, DSCP:6, ECN:2, totalLength:16; }; why can't I say this? IPv4Header h; auto& ecn = h.ECN; I'd expect the underlying code to

How to use binary flags in Core Data?

北城以北 提交于 2019-11-30 00:11:30
I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001" ? I'm also wondering if this is possible with a binary typed attribute ? NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL query (if you're using a SQLite backing store). You'll just have to try it. The syntax, however, is just what

Bit Shifting, Masking or a Bit Field Struct?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 22:54:12
问题 I'm new to working with bits. I'm trying to work with an existing protocol, which can send three different types of messages. Type 1 is a 16-bit structure: struct digital { unsigned int type:2; unsigned int highlow:1; unsigned int sig1:5; unsigned int :1; unsigned int sig2:7; }; The first two bits (type, in my struct above) are always 1 0 . The third bit, highlow, determines whether the signal is on or off, and sig1 + sig2 together define the 12-bit index of the signal. This index is split

Using Bitwise operators on flags

本小妞迷上赌 提交于 2019-11-29 20:50:41
I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All = 0x7 Say I receive the two flags Past and Future ( setFlags(PAST | FUTURE) ). How can I tell if Past is in it? Likewise how can I tell that Current is not in it? That way I don't have to test for every possible combination. If you want all bits in the test mask to match: if((value & mask) == mask) {...} If you want any single bit in the test mask to match: if((value & mask) != 0) {...} The difference is most apparent when you are testing a value for multiple things. To test for exclusion: if ((value & mask) == 0) { } First of all -

How to enforce the struct bit order with the GCC compiler?

萝らか妹 提交于 2019-11-29 17:53:34
问题 I was wondering if there is a GCC C Compiler directive that allows me to determine the bit order for packing of a structure? Something to the likes of: #pragma bit_order left The rationale for such a need is that I have the following structure: struct { union { unsigned char BYTE; struct { unsigned char B0: 1; unsigned char B1: 1; unsigned char B2: 1; unsigned char B3: 1; unsigned char B4: 4; }BIT; }ITEM; } myStruct; With this structure, I would like the compiler to pack it this way: Bit