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
-2
votes
2 answers

How to set/flip bitfields in C

I am trying to have a uint64_t bitfield be all set to 0. Then when I call the function within the given string, and it matches with the static global array that I have set it will flip the bit to 1. Currently I have the following code but for some…
h101
  • 31
  • 1
  • 5
-2
votes
2 answers

create array of int which is a bit field

I'm trying to create an array which at each index i have a bit field i tried this code: struct bitF { unsigned int x1:1; ... unsigned int xn:1; }intBF; typedef struct intBF *arr[]; int main(){ int i; unsigned int…
Rami Hassan
  • 149
  • 12
-2
votes
2 answers

Bitfields in C - aligning vs initializing

Being new to bitfields, I need some advice as to whats going on with various examples i've seen online. I'm wanting to use bitfields instead of bitmasks for readability, and maintenance ease later on for new ppl. This is a common means of declaring…
-2
votes
1 answer

Which bits are which in a struct with bitfields?

#include #include #include #include #include typedef struct AA { int a1:5; int a2:2; } AA; int main() { AA aa; char cc[100]; strcpy(cc, "0123456789"); memcpy(&aa, cc,…
myanl
  • 135
  • 1
  • 9
-2
votes
4 answers

values changed in bitfields in structure

Can anyone explain the output , how the value is stored and calculated? #include struct bitfield { unsigned f1:1; unsigned f2:2; unsigned :3; unsigned f4:4; unsigned f5:5; unsigned f6:6; unsigned…
akash
  • 1,801
  • 7
  • 24
  • 42
-2
votes
1 answer

How should I convert the endianness of a 14-bit bitfield?

If I know I am on a little-endian machine, how can I convert the endiannes of a 14-bit bitfield? struct { unsigned foo : 14, bar 2; } baz; I have a hunch that baz.foo = htons(baz.foo) will not work properly.
Matt
  • 21,026
  • 18
  • 63
  • 115
-3
votes
1 answer

Undefined but correct behavior in C++

The program below makes assumptions about the packing of bit-fields, type punning and object representation. In other words, it makes no pretense at portability. Still, it has the advantage of being fast. Can this program be said to be correct with…
user23952
  • 578
  • 3
  • 10
-3
votes
1 answer

Bit-Fields in C/C++: what is guaranteed, what is implementation-defined?

Reading https://en.cppreference.com/w/c/language/bit_field, are the following conclusions correct? Adjacent bit-fields have no padding in between (this seems to be differentin 6.7.2.1 of the C-standard). The placement of a bit-field within the…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
-3
votes
2 answers

type conversion from int to class behaving weirdly

So. I am trying to convert a uint16_t (16 byte int) to class. To get the class member varaible. But it is not working as expected. class test{ public: uint8_t m_pcp : 3; // Defining max size as 3 bytes bool m_dei : 1; uint16_t m_vid :…
rashi
  • 3
  • 2
-3
votes
1 answer

Structures with variable number of members and bitfields from the array

I have many structures, which looks like: struct S { float a; uint8_t b; uint8_t c : 4; uint8_t d : 2; uint8_t e : 1; uint8_t f : 1; uint8_t g : 1; ... } __attribute__((packed)); Number of members, their names, type and size of bit fields…
Gusya
  • 1
  • 1
-3
votes
2 answers

How do I interpret an #if/#else bitfield? (VC++ noob)

Background: I have a buggy program that decompiles a specific set of files. The bugs are NOT related to the records it is decompiling, just some ancillary things. I am porting the program to AHK to clean up the bugs and add some…
Qriist
  • 11
-3
votes
2 answers

need help - bit-field conversion

I want to convert strings to bit-fields.Also,convert them to binary and then use. Need help with this..help me ..
user46646
  • 153,461
  • 44
  • 78
  • 84
-4
votes
1 answer

bit fields in c programming

Here in this code #include void main() { struct bits{ unsigned a:5; unsigned b:5; char c; int d; } bit1; printf("%d",sizeof(bit1)); } the output is 5 please explain how did 5 come
Pritul Dave
  • 189
  • 1
  • 11
-4
votes
1 answer

Can someone explain me why do we get the following output?

#include int main(void) { struct str { int i: 1; int j: 2; int k: 3; int l: 4; }; struct str s; s.i = 1; s.j = 2; s.k = 5; s.l = 10; printf(" i: %d \n j: %d \n k: %d \n l: %d \n", s.i, s.j, s.k, s.l); getchar(); return…
-4
votes
1 answer

Size of a structure having unsigned short ints

I was surfing in one of our organisational data documents and I came across the following piece of code. struct A { unsigned short int i:1; unsigned short int j:1; unsigned short int k:14; }; int main(){ A aa; int n = sizeof(aa); cout <<…
1 2 3
57
58