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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
33 views

C lang Bit fields described by MemoryLayout in Java

How can a C language struct using Bit fields be described using Java MemoryLayout API (part of JEP 442: Foreign Function & Memory API (Third Preview))? For example how to describe C struct ...
czerny's user avatar
  • 15.5k
0 votes
1 answer
32 views

Python how to mask and int with bitfields

Sorry if this is a beginner question - my experience with bitfields is zero. I have a series of 25 flags represented by Bitfields and an session integer which represents any flags set at this time. ...
Byte Insight's user avatar
1 vote
0 answers
104 views

C++ 14 - union - Is it legal to access inactive member?

After read a lot about it, I'm confused about if it is legal or not accessing to inactive member of an union starting from C++14. I understood it is undefined behaviour up to C++11. Some references: ...
Manuel Tonella's user avatar
0 votes
1 answer
66 views

Union of unsigned short and anonymous bitfield

Minimal example: union v1_t { struct { unsigned int d1 : 4; unsigned int d2 : 4; unsigned int : 8; }; unsigned short data; }; union v2_t { unsigned short data; ...
deponovo's user avatar
  • 1,319
0 votes
2 answers
78 views

Reserve memory in base class to be used in derived class c++

I am writing some low level abstractions for communicating with some chip via SPI and I have created registers abstraction to avoid tricky bit manipulation. I thought that i may create interface ...
lukasz luko's user avatar
0 votes
1 answer
88 views

Why can't my code correctly output 'C' information in the VS Code environment, but it works with GCC? It might be due to issues related to bitfields

false true #include<iostream> using namespace std; enum Leve{Freshman,Sophomore,Junior,Senior}; enum Grade{A,B,C,D}; class student { public: student(unsigned number, Leve leve, Grade grade):...
Eli's user avatar
  • 3
0 votes
1 answer
71 views

Defined behaviour for union with 24-bit and 8-bit vars

I'm trying to find the best way to pack a 24-bit and 8-bit unsigned integer together into 32 bits without requiring bit-shifts to extract data. Unions immediately came to mind with a simple approach ...
Jan Kuhlmann's user avatar
2 votes
2 answers
147 views

Struct is padded to 8 bytes when 6 bytes seem sufficient

In the write-up The Lost Art of Structure Packing, the author introduces struct foo6 (...) in chapter 6 : struct foo6 { short s; char c; int flip:1; int nybble:4; int septet:7; }; ...
bdmt's user avatar
  • 21
3 votes
2 answers
164 views

Trying to use std::bit_cast with a bitfield struct. Why is it not constexpr?

I'm trying to get a bitmask from a bitfield struct, at compile time. One of the tricks that I tried, which looks promising to me, is using std::bit_cast, because it is supposed to be constexpr. My ...
sh-'s user avatar
  • 991
0 votes
0 answers
24 views

Bitfield Order in struct [duplicate]

Running the below code give me this result: 01010101. Does this mean the machine places these bits from right to left? Is there any way to tell it to reverse the order? i.e the first bit in the struct ...
HV16's user avatar
  • 39
3 votes
1 answer
90 views

Is this a GCC bug with storing result of bitwise operation on uint128_t in a bitfield

This code works with GCC with less than -O2 optimizations or in clang. constexpr uint16_t KEY_BITS = 70; constexpr unsigned __int128 KEY_BITS_MASK = (((unsigned __int128) 1) << KEY_BITS) - 1; ...
mihovg93's user avatar
0 votes
1 answer
58 views

Byte order in C bit field? [duplicate]

Consider uint32_t n = 0x12345678; it stores in BE machine or LE machine, like the picture shows; now I have a structure defined like this struct DATA { uint32_t a : 24; uint32_t b : 8; }; int ...
FLYFLY's user avatar
  • 57
5 votes
1 answer
147 views

Placing a structure into memory smaller than it

I'm optimizing a compression algorithm, which uses a structure that spans 2 bytes. But there are times when I'd like it to interpret just 1 byte, as its members that (I expect) to map onto the 2nd ...
Anne Quinn's user avatar
  • 12.7k
1 vote
4 answers
96 views

How to construct a C struct/union with a very odd arrangement of bitfields?

I have the following layout of memory (pseudo code): struct { union { fieldA : 45; struct { fieldB1 : 12; fieldB2 : 33; } } fieldC : 19; ...
RBH's user avatar
  • 11
-1 votes
2 answers
83 views

Why does unsigned 3-bit bitfield wrap around -7 to 1

#include <stdio.h> struct test{ unsigned int a:3; }; int main (int argc, char *argv[]) { struct test b; b.a = -7; // implicit truncation to 1 return 0; } -7 is a four bit number ...
Narayan's user avatar
  • 13
2 votes
2 answers
91 views

How to properly represent arrays of non full byte numbers in c++?

I wanted to implement the FAT12 specification in C++, where the FAT is an array of 12-Bit numbers. Because types can only have full byte size I tried to use bitfields in structs to have a pair of two ...
wediaklup's user avatar
1 vote
0 answers
23 views

Consecutive struct bitfields not behaving as expected. Doesn't appear to be a padding issue

I'm experimenting with the SNTP protocol. While attempting to modify the message struct I ran into issue I cannot explain. Despite the fact that the first 32-bit word contains 6 fields, the struct I ...
mreff555's user avatar
  • 1,059
0 votes
4 answers
140 views

Is there a way to detect padding bits in a bitfield?

I extensively utilize bit-fields in my C++ embedded application, and I have encountered a problem. Below is an example showcasing my usage: struct { uint8_t /* Reserved */ : 3; uint8_t foo : 3;...
Caglayan DOKME's user avatar
-1 votes
2 answers
68 views

Do I need to handle endianess over an 8 bits bit-fields struct on a 32 bit architecture?

I am doing some embedded software on a 32 bits architecture (precisely AVR32). On this software I am retrieving a flag, coded on one byte, from an external peripheral connected via I2C. Each bits of ...
Amaury's user avatar
  • 73
1 vote
1 answer
69 views

Cannot understand how the gdb memory dump of my struct with bitfields is correct given my struct initialisation

I have designed a Packet struct for a custom networking protocol as follows: typedef struct { uint8_t src; uint8_t dest; uint8_t len; uint8_t flag; //bitfields allocated R-L? gcc ...
Armaan Chowfin's user avatar
4 votes
1 answer
110 views

will adding a new bit-field to my C-struct break binary-compatibility?

I have inherited some code that makes use of bitfields in a struct: typedef _my_flags { unsigned int x_ida:1; unsigned int x_foo:6; unsigned int x_bar:6; unsigned int x_bonzo:6; ...
umläute's user avatar
  • 29.3k
0 votes
0 answers
19 views

Padding byte in Struct

#include <stdio.h> struct { short a : 2; short b : 15; char c; }abc; int main() { printf("%d",sizeof(abc)); return 0; } The ...
Vetmark's user avatar
-3 votes
1 answer
176 views

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 ...
user23952's user avatar
  • 578
0 votes
0 answers
46 views

how to assign for loop index to bit field

How to design a code to pass the bit location. typedef struct { A :1; B :1; C :1; D :1; }bit; void main() { bit Bit_Pass = 0; uint8 a = 0; for (a = 0; a<4; a++) { ...
StackUser's user avatar
0 votes
1 answer
201 views

What is the proper way to std::byteswap member bitfields in C++

I need to convert struct fields that came via network as raw bytes from big-endian to little-endian. Consider the following struct: struct [[gnu::packed]] Header { std::uint8_t magic_value:4; ...
blonded04's user avatar
  • 351
1 vote
3 answers
108 views

Why can't I make an array of structs with bitfield?

Edit: I had to use union for that purpose specifically, because the chapter is about them. The fragment of the first task which the one I speak of is based on: Design a union named byte_set, which ...
lewando54's user avatar
-2 votes
2 answers
454 views

ctypes.Structure with bit fields are not setting values correctly

I have the following in C #include <stdio.h> struct Demo { char f1:8; int f2:21; char f3:2; char f4:1; }; int main() { struct Demo d = {1, 15, 3, 1}; printf("%ld\n&...
deltaWeeb's user avatar
4 votes
2 answers
58 views

successive unnamed field of width zero in bit-field does not change size of struct

Here is a struct. struct { unsigned int a : 8; unsigned int : 0; unsigned int f : 1; } A; sizeof struct A here is 8 bytes. I understand that. But now if I put one more unnamed field of ...
user9026's user avatar
  • 854
1 vote
1 answer
188 views

Behavior of union with different data types

I have two unions declared typedef union { struct { unsigned short n1 : 4; unsigned int n2 : 4; } s; unsigned int val; } unionA; typedef union { struct { ...
torres's user avatar
  • 23
2 votes
0 answers
56 views

is there a method to make a struct inside another struct take less then one byte?

Hi I'm defining a chip instruction, and would like to use bit-field so it is easy to operate. The instruction may looks like the following: struct __attribute__((__packed__)) Opcode { enum { ...
Shore's user avatar
  • 889
1 vote
1 answer
47 views

how to pack the size of struct contains bitfield struct in it?

Hi I'm defining the following structure in C struct __attribute__((__packed__)) Input { enum { I_PE_left = 0, I_PE_up = 1, I_PE_right = 2, I_PE_down ...
Shore's user avatar
  • 889
0 votes
0 answers
17 views

C bit field and the memory layout

I am writing a program that parses the IP header, and the structure of the IP header is defined as follows. struct ip_hdr { uint8_t ihl : 4; uint8_t version : 4; uint8_t tos; uint16_t ...
XM Zg's user avatar
  • 109
0 votes
3 answers
130 views

Why or when should I use bit shifts in enum like this? [closed]

While studying enums, I have seen some people use them in combination with bit shifting. Being a beginner and unsure of correct usage, I would appreciate advice. So here are 2 examples. #define BIT(x) ...
Mateo Forr's user avatar
0 votes
1 answer
79 views

What is the union of bitfield equivalent in Dart?

I'd like to achieve the following C code in Dart: union AuxiliaryHardwares { uint16_t all = 0; struct { hasRs485: 1; hasCanbus: 1; hasRelay0: 1; hasRelay1: 1; }; }; ...
Stéphane de Luca's user avatar
1 vote
3 answers
82 views

C: Typecasting with bitfields reverses values?

I am trying to cast a byte stream (raw data from serial port) into a structure for ease of use. I have managed to replicate the problem in a minimal working example: #include <stdio.h> typedef ...
First User's user avatar
2 votes
1 answer
116 views

Mixing colors with bitfields in C with bitwise operators?

I was reading the Wikipedia entry on bit-fields, and saw how one might use binary numbers to represent primary colors and combine them using bitwise OR (|) operators. I want to check if one color is ...
First User's user avatar
0 votes
1 answer
151 views

Characteristics of bit-Fields in C++

Reading https://en.cppreference.com/w/cpp/language/bit_field, are the following conclusions correct? whether adjacent bit-fields have no padding in between is implementation-defined (this reads ...
wimalopaan's user avatar
  • 5,060
-3 votes
1 answer
480 views

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-...
wimalopaan's user avatar
  • 5,060
0 votes
1 answer
84 views

g++ question about endianism that *should* work?

I thought this should work, but I'm obviously wrong, but I don't know why :-) Assume I had the following bytes from the network 0x03 0x02. In my head, I expect it to be converted to little endian and ...
user500123's user avatar
-1 votes
1 answer
70 views

Why the output is 4 and not 16 in this C code?

I dont understand why the output of this code is 4 and not 16, Assuming that unsigned int takes 4 bytes and long int takes 8 bytes. Any help? #include <stdio.h> struct test { unsigned int x;...
Ismail Kazdar's user avatar
1 vote
2 answers
91 views

Using bitfield-structs in binary de-serialization: is layout guaranteed on GCC?

So, I'm writing a struct that's going to be used for de-serializing a binary stream of data. To get the point across, here is a cut-down version: typedef struct { bool flag1 : 1; bool flag2 : ...
Rui Oliveira's user avatar
0 votes
1 answer
359 views

When should someone use bit-field on a boolean value?

I have been researching some code in c++ in unreal engine and I came across that they have on a header file 4 booleans declared as: bool bIsEvaluating : 1; bool bIsStopping : 1; bool bIsBeginningPlay :...
Toulis's user avatar
  • 120
2 votes
2 answers
97 views

Create an Enum with a bitfield

First a bit of context : I want to process various documents and detect for each one which european countries have been mentioned. Example : EU_COUNTRIES = ( "Austria", "...
dude's user avatar
  • 53
0 votes
1 answer
166 views

Assembly x86 MASM - Problem of extracting 5 bits from memory

I have a problem to solve and I have no idea on how to go about it. I'm asking for a general idea on how to go about this. I have a memory address, in ESI. Memory represents some kind of simplified ...
roditu's user avatar
  • 59
0 votes
1 answer
67 views

Am I using memcpy wrong?

I'm trying to use memcpy to copy part of an unsigned int to another unsigned int within the same struct I made. But my program only prints the first printf statement and then says: Process returned -...
gloriousnoob100's user avatar
1 vote
1 answer
68 views

Why can't a constant address be computed in a bitfield?

I'm trying to implement a constant linked DMA descriptor list (in ROM) on an Silabs EFR32BG22, where the last descriptor links to another descriptor located in RAM. I'm using arm-none-eabi-gcc 10.2 (...
gietljohannes's user avatar
0 votes
1 answer
54 views

How is this bit fields based structure calculating 1. the structure's size 2. the difference between pointer values?

Code #include <stdio.h> struct test { unsigned int x; // consider int as 4 bytes long int y : 33; // consider long as 8 bytes unsigned int z; }; int main() { struct test t; ...
Pragati Bhattad's user avatar
0 votes
2 answers
105 views

C++ convert an unsigned int in range [0, 2^n) to signed int in range [-2^(n-1), 2^(n-1) )

At the outset, I realize what I did was bad. I relied on what is now (at least) undefined behavior, if not explicitly forbidden. It used to work, and I thought I was being clever. Now it doesn't and I'...
jwm's user avatar
  • 1,594
-1 votes
1 answer
95 views

How can I access an element of a bit-field structure in C at runtime?

I have a structure coloring with bit fields defined in C, and one such structure called color struct coloring { unsigned int a : 3; unsigned int b : 3; unsigned int c : 3; unsigned int ...
Excited and Confused's user avatar
-3 votes
2 answers
82 views

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 ...
rashi's user avatar
  • 3

1
2 3 4 5
18