Questions tagged [bit-fields]

A bit field is used to compactly store multiple logical values as a short series of bits where each of the single bits can be addressed separately.

A bit field is used to represent and store a set of known width and logically grouped set of values. These fields can then be addressed individually in the code. A common use of such a construct is flags.

In language such as C and C++, bit fields can also be used to abstract and interop with specific hardware.

857 questions
-1
votes
1 answer

Arrays of enum's packed into bit fields in MSVC++

Unsing MS Studio 2022 I am trying to pack two items into a union of size 16 bits but I am having problems with the correct syntax. The first item is an unsigned short int so no problems there. The other is an array of 5 items, all two bits long. So…
-1
votes
1 answer

C++ struct sizes are not making sense

When I compile and run the following code: struct preferences { bool likesMusic : 1; bool hasHair : 1; bool hasInternet : 1; bool hasDinosaur : 1; unsigned int numberOfChildren : 4; }; int main() { struct preferences homer; …
-1
votes
1 answer

Restoring bitfield truncated addresses

I would like to save some space by truncating my pointer addresses by 1 byte. My pointers are guaranteed to be 16-byte memory aligned. Thus, I can guarantee that those 4 bottom bits can be truncated. typedef struct node_t { uintptr_t prev : 29; …
Peabrain
  • 519
  • 2
  • 12
-1
votes
2 answers

How to read the syntax of a C++ Bitfield definition?

I need some help in understanding the syntax of a Bitfield definition. I read the Microsoft documentation page on it but the example there still leaves me with my question. Given a Bitfield and main method like this: struct { unsigned short…
TMOTTM
  • 3,286
  • 6
  • 32
  • 63
-1
votes
4 answers

can't understand bit fields in C

For about 3-4 hours and start reading about bit fields in C and I can't understand how they work. For example, I can't understand why the program has the output: -1, 2, -3 #include struct REGISTER { int bit1 : 1; int : 2; …
DaniVaja
  • 211
  • 1
  • 7
-1
votes
1 answer

How to set a member of a bitfield created with the bitfield crate?

I'm trying to replicate the following C bitfield and functionality in Rust: typedef struct { uint8_t directive_code : 4; uint8_t directive_subtype_code: 4; uint8_t condition_code: 4; uint8_t delivery_code: 2; uint8_t…
tyler124
  • 613
  • 1
  • 8
  • 20
-1
votes
2 answers

Check that all bits are set except the Least Significant Bit by using masks

I'm just experimenting with bitfields in C(beginner). Let's say I have an integer: uint8 myBitfield How do I check that all bits except the Least Significant Bit are set. I thought this shall work: if ((myBitfield & 0xFE) == 1) // 0xFE =…
JohnDoe
  • 825
  • 1
  • 13
  • 31
-1
votes
1 answer

what does enum DescriptorType DescriptorType :8; mean in C code?

This is part of the code. I can not understand what does enum DescriptorType DescriptorType :8; mean? Also what does __attribute__ ((__packed__)); mean here? enum DeviceClass { DeviceClassInInterface = 0x00, DeviceClassCommunications = 0x2, …
Brian Lee
  • 55
  • 4
-1
votes
2 answers

How to Write Bitfield to a Binary File

Lets say I have a bitfield that totals to 32 bits. I want to output these into a binary file, lets call it "binary.bin". How do I go about this without crashing visual studio? I have found so little information on things like this from previous…
-1
votes
3 answers

Bit Field Struct to int in C++

I have a struct with same type members in it. I am trying to convert it into uint8_t type. I am able to do that but cannot see the output please tell me where I am going wrong. Also I know there are another ways to do it? I am trying to do it this…
Mandeep
  • 335
  • 3
  • 13
-1
votes
1 answer

C/C++ -- Bitfields as booleans?

I'm currently dealing with a SigFox-based IoT device which can send messages with a payload up to 12 bytes in size. This means that the chip manufacturer usually has to get creative. I'm currently dealing with a message that looks like this:…
ItWillDo
  • 468
  • 4
  • 9
-1
votes
1 answer

C++: Unable to set type int to non-scalar after bitwise functions

I have two bitfields: (1) one to handle the frame (header), (2) the other to handle a subframe within a frame (identityFieldO2M). union header { unsigned char arr[16]; // 128 bytes allocated BitFieldMember<0, 1> SOF; BitFieldMember<1,…
SamJ
  • 43
  • 7
-1
votes
1 answer

Can bifields be compared, using <,>,=?

I am attempting to use a bit-field to store a series of counters that are being used to make triangles for graphics, since I only need values 0,1,2 for some of them and dont want to waste memory. What the code should do is start from bf.vertIndex =…
-1
votes
2 answers

Is it OK to use bitfields in dynamically allocated structure in C?

I'm going to implement a singly linked list program with bitfield in its structure, something like this: typedef struct large { unsigned number :4; struct large *next; } large; int main() { large *g; g=(large…
Tuesday
  • 55
  • 1
  • 7
-1
votes
3 answers

Struct variable doesn't changed by assignment

struct st { int a1 : 3; int a2 : 2; int a3 : 1; } void main(void) { x.a3 = -1; if (x.a3 == -1) printf("TRUE\n"); else printf("FALSE\n"); x.a3 = 1; if (x.a3 == 1) printf("TRUE\n"); else…
BobKim
  • 9