bit-fields

C++ bool array as bitfield?

Hi! let's say i need to store 8 bools in a struct, but i want to use for them only 1 byte together, then i could do something like this: struct myStruct { bool b1:1; bool b2:1; bool b3:1; bool b4:1; bool b5:1; bool b6:1; bool b7:1; bool b8:1; }; and with this i could do things like myStruct asdf; asd...

Bit Shifting, Masking or a Bit Field Struct?

Hi, 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 abov...

force a bit field read to 32 bits

I am trying to perform a less-than-32bit read over the PCI bus to a VME-bridge chip (Tundra Universe II), which will then go onto the VME bus and picked up by the target. The target VME application only accepts D32 (a data width read of 32bits) and will ignore anything else. If I use bit field structure mapped over a VME window (nmap'd...

How do I parse out n-bit elements from a byte addressable array

I have a data stream that is addressable only in 8-bit bytes, I want to parse it out into 6-bit elements and store that into an array. Is there any best known methods to do this? 11110000 10101010 11001100 into an array like 111100|001010|101011|001100 (can have zero padding, just needs to be addressable this way) and the dat...

Please help me find the official name of this programming approach.

I [surely re] invented this [wheel] when I wanted to compute the union and the intersection and diff of two sets (stored as lists) at the same time. Initial code (not the tightest): dct = {} for a in lst1: dct[a] = 1 for b in lst2: if b in dct: dct[b] -= 1 else: dct[b] = -1 union = [k for k in dct] inter = [k for k in dct...

Parsing a bit field parameter, how to "discard" bits in an unsigned long?

Hey all, First of all, I want to know if this is possible: let's say I have an unsigned long which contains some abritrary unsigned shorts, which may or may not be in the number. For example: unsigned short int id1 = 3456, id2 = 30998; unsigned long long bitfld = id1|id2; Can the other 2 fields be assumed as 0? An...

Questions about C bitfields

Is bitfield a C concept or C++? Can it be used only within a structure? What are the other places we can use them? AFAIK, bitfields are special structure variables that occupy the memory only for specified no. of bits. It is useful in saving memory and nothing else. Am I correct? I coded a small program to understand the usage of bit...

C/C++ bitfields versus bitwise operators to single out bits, which is faster, better, more portable?

I need to pack some bits in a byte in this fashion: struct { char bit0: 1; char bit1: 1; } a; if( a.bit1 ) /* etc */ Or: if( a & 0x2 ) /* etc */ From the source code clarity it's pretty obvious to me that bitfields are neater. But which option is faster? I know the speed difference won't be too much if any, but as I can use any of th...

In C#, how to easily map enum flags from one type to another?

Also see the updates at the end of the question... Given the following situation: [Flags] enum SourceEnum { SNone = 0x00, SA = 0x01, SB = 0x02, SC = 0x04, SD = 0x08, SAB = SA | SB, SALL = -1, } [Flags] enum DestEnum { DNone = 0x00, DA = 0x01, DB = 0x02, DC = 0x04, DALL = 0xFF, } I...

Bitfield enum extension method to return dictionary of included values

Enums can sure be confusing. I am trying to create an extension method on the Enum type that will take a value and return the names of all the bits that match. Given: [Flags] public enum PlanetsEnum { Mercury=1, Venus=2, Earth=4, Mars=8, Jupiter=16, //etc.... } I would like to create an extension method that return a dic...

Bit-fields of type other than int?

I have a code which uses bit-fields declared as follows typedef struct my{ const char *name; uint8_t is_alpha : 1; uint8_t is_hwaccel : 1; uint8_t x_chroma_shift; uint8_t y_chroma_shift; } mystr; uint8_t is typedef'ed to unsigned char. Building the code in MS-VS 2008 using this bit fields gives a warning a...

Converting Bit Field to int

Hi, I have bit field declared this way: typedef struct morder { unsigned int targetRegister : 3; unsigned int targetMethodOfAddressing : 3; unsigned int originRegister : 3; unsigned int originMethodOfAddressing : 3; unsigned int oCode : 4; } bitset; I also have int array, and i want to get int value from this array...

Tips on redefining a register bitfield in C

I am struggling trying to come up with a clean way to redefine some register bitfields to be usable on a chip I am working with. For example, this is what one of the CAN configuration registers is defined as: extern volatile near unsigned char BRGCON1; extern volatile near struct { unsigned BRP0:1; unsigned BRP1:1; unsigned...

How slow are bit fields in C++

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 ResetFla...

struct bitfield max size (C99, C++)

Hello What is maximal bit width for bit struct field? struct i { long long i:127;} Can I define a bif field of size 128, 256 bit or larger? There are some extra-width vector types, like sse2, avx registers. ...

How can I get bitfields to arrange my bits in the right order?

To begin with, the application in question is always going to be on the same processor, and the compiler is always gcc, so I'm not concerned about bitfields not being portable. gcc lays out bitfields such that the first listed field corresponds to least significant bit of a byte. So the following structure, with a=0, b=1, c=1, d=1, you ...

Bit fields in C ?

HI all, Is there anyway by which we can declare variable specifying bit fields on those which are not any members of structures or unions.If not,then is there anyway by which we can just declare a variable by specifying the number of bits it is permitted to use. Thanks maddy ...

GCC, -O2, and bitfields - is this a bug or a feature?

Today I discovered alarming behavior when experimenting with bit fields. For the sake of discussion and simplicity, here's an example program: #include <stdio.h> struct Node { int a:16 __attribute__ ((packed)); int b:16 __attribute__ ((packed)); unsigned int c:27 __attribute__ ((packed)); unsigned int d:3 __attribute__ ((packe...

What does 'unsigned temp:3' means

Hi, I'm trying to map a C structure to Java using JNA. I came across something that I've never seen. The struct definition is as follow, struct op { unsigned op_type:9; //---> what does this means? unsigned op_opt:1; unsigned op_latefree:1; unsigned op_latefreed:1; unsigned op_attached:1; unsigned op_spa...

Can Microsoft store three-valued fields in a single bit?

I'm completely ignorant of SQL/databases, but I was chatting with a friend who does a lot of database work about how some databases use a "boolean" field that can take a value of NULL in addition to true and false. Regarding this, he made a comment along these lines: "To Microsoft's credit, they have never referred to that kind of field...