Linked Questions
20 questions linked to/from When to use bit-fields in C
0 votes
0 answers
646 views
: symbol in c structure and it uses [duplicate]
Whats does the : symbol means in c structure. The code below supposed to show output 12 (size of int=4,size of structure=sum of each structure member) But it is showing 8 as output. #include<...
-4 votes
1 answer
166 views
What does this short C++ code mean? [duplicate]
This was a code snippet from a test question. The question was what the size of S would be. struct S { char a : 4; unsigned char b : 3; signed char : 2; char c : 1; char d : 5; }...
1 vote
1 answer
136 views
What is : operator in Embedded C [duplicate]
In a project of STM32, I came through a code like this : typedef union { struct __attribute__ ((packed)){ uint8_t ModePin0 :1; uint8_t ModePin1 :1; ...
24 votes
4 answers
7k views
Are there reasons to avoid bit-field structure members?
I long knew there are bit-fields in C and occasionally I use them for defining densely packed structs: typedef struct Message_s { unsigned int flag : 1; unsigned int channel : 4; ...
11 votes
6 answers
3k views
How can I use Bit-Fields to save memory?
This is about ANSI-C (C90). This is what I know: I can directly tell the compiler how many bits I want for a specific variable. If I want 1 bit which can have the values zero or one. or 2 bits for ...
4 votes
3 answers
5k views
How to declare different size variables
Hi I want to declare a 12 bit variable in C or any "unconventional" size variable (a variable that is not in the order of 2^n). how would I do that. I looked everywhere and I couldn't find anything. ...
0 votes
2 answers
8k views
How can I clear a certain number of bits in C?
I know there is another post here that is how to clear a single bit, but how about a whole byte? For example, if I had 00000000 00000000 00101100 11000100 and I wanted to clear the second chunk so it ...
6 votes
1 answer
2k views
What is a non-field member of a structure or union?
From K&R The C Programming Language: A non-field member of a structure or union may have any object type. A field member (which need not have a declarator and thus may be unnamed) ...
0 votes
1 answer
3k views
how to read ctypes structures from file
I'm trying to learn more about PE files structures and also ctypes structures. I got a couple of questions: Given this simple declaration: from ctypes import * class ImageDosHeader(Structure): ...
0 votes
1 answer
1k views
Can I make bools bit fields?
Is this legal? I read that you can only use integers as bitfields, but does this apply to the bool/_Bool types? Is this OK, or is this undefined behavior somehow? struct MyStruct { // ... bool ...
0 votes
2 answers
953 views
C Programming - What is 2U or 8U in struct
Code from this link: https://github.com/openwch/arduino_core_ch32/blob/main/libraries/USBPD_SINK/src/usbpd_def.h I am looking at the previous file. I did some search and found something about unsign ...
2 votes
4 answers
2k views
Multiple adjacent bit fields in C++
I saw multiple adjacent bit fields while browsing cppreference. unsigned char b1 : 3, : 2, b2 : 6, b3 : 2; So, What is the purpose of it? When and where should I use it?
3 votes
2 answers
431 views
Usability of bitfields
There's a lot of advice out there saying not to use bitfields but to do the bit arithmetic manually (e.g., When to use bit-fields in C?) because bitfield layouts are implementation-defined. Is this ...
0 votes
2 answers
449 views
uint8_t data types initialisation [duplicate]
Recently I stumbled upon a code written like this: typedef struct { uint8_t TC0_WG0 :2; uint8_t TC0_CS :3; } Timer0; What I wanted to know is what does the part that says :2; & :3; ...
1 vote
2 answers
154 views
How this C struct outputs the value?
I am beginner in C and can't understood how this piece of code is working: struct marks{ int p:3; int c:3; int m:2; }; void main(){ struct marks s={2,-6,5}; printf("%d %d %d", s.p, s.c, s....