bit-fields

Clarification about Bit-field ordering semantics in C

让人想犯罪 __ 提交于 2020-01-29 14:38:09
问题 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

How to work with bitfields longer than 64 bits?

[亡魂溺海] 提交于 2020-01-23 05:49:06
问题 Question says it all. If I have this for a 96-bit field: uint32_t flags[3]; //(thanks @jalf!) How do I best go about accessing this, given that my subfields therein may lie over the 32-bit boundaries (eg. a field that runs from bit 29 to 35)? I need my accesses to be as fast as possible, so I'd rather not iterate over them as 32-bit elements of an array. 回答1: [This answer is valid for C (and by extension, for C++ as well).] The platform-independent way is to apply bit-masks and bit-shifts as

Bit field specialization in python

二次信任 提交于 2020-01-16 06:14:26
问题 Here is a code in C++: void sign_extending(int x) { int r; // resulting sign extended number goes here struct {signed int x:5 ;} s; r = s.x = x; cout << r; } void Run() { int x=29; // this 29 is -3 ( 11101 ) in 5 bits // convert this from using 5 bits to a full int sign_extending(x); } The output of this code is -3. When i try to reproduce this code in python the bit field of 11101 is generated but when the answer is converted to an int the answer of 29 is given . the following is code of

Getting negative value with bit-fields

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 11:17:31
问题 I have a question related to bit-fields in C. Here I have such a structure: struct Register { int bit:1; }; int main(void) { struct Register bit = {1}; printf("\nbit = %d", bit.bit); return 0; } Can you please explain me why do I get: bit = -1 回答1: If you're working with bitfields, you should use unsigned int . signed int is a problem for bit-fields. 回答2: use unsigned int , it stores 0 and 1, struct Register { unsigned int bit:1; }; int main(void) { struct Register bit = {1}; printf("\nbit =

Pushing boolean values of an array to uint8 bitfield

こ雲淡風輕ζ 提交于 2020-01-05 07:23:12
问题 Let's suppose I have: myArray[7] = {TRUE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE} uint8 mybitfield; What is the most efficient way to "push" those values to an uint8 bitfield with 0=FALSE , 1=TRUE So that mybitfield is represented as: [1,0,0,1,1,0,0,0] (The Least Significant Bit is not considered and will be always 0). Thanks! 回答1: As already noted, you have to iterate over the bits individually, for example: int myArray[7] = {TRUE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE}; uint8_t bitfield = 0; for (int

Representing union bitfields using c#'s StrucLayout and FieldOffset

回眸只為那壹抹淺笑 提交于 2020-01-03 11:45:32
问题 I understand that in order to represent unions in C# I need to use StructLayout[LayoutKind.Explicit)] and [FieldOffset(x)] attribut to specify the byte offset inside the union. However, I have a following union I want to represent and FieldOffset attrib only offset by size of a byte. union _myUnion { unsigned int info; struct { unsigned int flag1:1 // bit 0 unsigned int flag2:1 // bit 1 unsigned int flag3:1 // bit 2 unsigned int flag4:1 // bit 3 unsigned int flag5:1 // bit 4 unsigned int

Will this bitfield work the way I expect?

让人想犯罪 __ 提交于 2020-01-02 06:49:06
问题 I've been doing some reading about bitfields in C, how the C standard doesn't enforce any particular ordering of the fields in a machine word, and so on. I hope this question appropriately fits the format of SO. My question is whether my struct (definition following) will actually perform in the way I expect. Here's the definition I came up with, and then I'll discuss what I want: typedef enum { STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR } __attribute__ ((packed)) State; typedef struct

Unresolved external symbol __aullshr when optimization is turned off

霸气de小男生 提交于 2020-01-02 05:22:06
问题 I am compiling a piece of UEFI C code with Visual Studio 2015 C/C++ compiler. The compiler is targeting IA32 , not X64. When turning on the optimization with "/O1", the build is OK. When turning off the optimization with "/Od", the build gives below error: error LNK2001: unresolved external symbol __aullshr According to here, there's an explanation why this kind of functions can be called implicitly by the compiler: It turns out that this function is one of several compiler support functions

c++ bit fields and -Wconversion

試著忘記壹切 提交于 2020-01-01 08:52:54
问题 -Wconversion is producing warnings when I assign a value to a bit field with g++. Source file: struct Foo { public: unsigned int x : 4; unsigned int y : 9; unsigned int z : 17; }; int main(int, char**) { int a = 12; Foo f; f.x = a; f.x = (unsigned int)a; f.x = (unsigned char)a; f.x = (unsigned short)a; f.x = (unsigned)a; f.y = a; f.y = (unsigned int)a; f.y = (unsigned char)a; // no warning, sizeof(char) < 9 f.y = (unsigned short)a; f.y = (unsigned)a; f.z = a; f.z = (unsigned int)a; f.z =

Memory layout of struct having bitfields

不羁的心 提交于 2019-12-28 06:33:21
问题 I have this C struct: (representing an IP datagram) struct ip_dgram { unsigned int ver : 4; unsigned int hlen : 4; unsigned int stype : 8; unsigned int tlen : 16; unsigned int fid : 16; unsigned int flags : 3; unsigned int foff : 13; unsigned int ttl : 8; unsigned int pcol : 8; unsigned int chksm : 16; unsigned int src : 32; unsigned int des : 32; unsigned char opt[40]; }; I'm assigning values to it, and then printing its memory layout in 16-bit words like this: //prints 16 bits at a time