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
0
votes
1 answer

A bit field of enumeration type and a value stored to it

I wrote the following and I expected that 16 would be printed. #include enum E : long { e = 16 }; struct X { E e : 5; }; X x; int main(){ x.e = E::e; std::cout << static_cast(x.e) << std::endl; } DEMO But it wasn't. I got…
user2953119
0
votes
2 answers

Merging 13 bits array into an array of unsigned char

I'm writing an algorithm that compresses data (LZSS) and it requires me to have two 13-bit values which I'll have to later merge together. In some cases, however, I don't need 13 bits; 8 are enough. For this purpose I have a structure like…
0
votes
1 answer

What's the use of initializing blank bitfields

So I was learning about bitfields, and when I stumbled upon this link, I saw unsigned char :0; // start a new byte I'm wondering why that would be used, since it's wasting memory. Does it have a practical use? Where would it be used…
JoeyChor
  • 63
  • 1
  • 7
0
votes
0 answers

MySQL datatypes - UNSIGNED INTEGER vs BIT(32)

I need a bitfield column in my MySQL database and I heard from the datatype BIT(M) where M is the number of bits and of course there is the datatype UNSIGNED INTEGER which holds 32 bits (0 - 2³²-1). Of course, for a bitfield, BIT is better because I…
MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
0
votes
4 answers

confusion with bitfield in C

In the code below: #include struct { int Member1 : 3; int Member2 : 1; }d2; int main(){ d2.Member1 = 7; printf("%d\n",d2.Member1); return 0; } The result is -1, why is it? What's the binary value of the d2 now?
user3289218
  • 87
  • 1
  • 5
0
votes
2 answers

Reading n-bit elements from a data stream in C

Given a data stream in C, I need to read the nth element which is x bits wide. x can vary from 1-64. How do I do this in C? I tried some bit fiddling but could not come up with a solution. For example, for a data stream 01101010 11010101 11111111…
0
votes
4 answers

Binary "bulk" serialization of bitfields

I have objects with lots of boolean properties, so I am using bitfields to pack the properties more compact. I also want to be able to serialize and deserializa those properties in a compact way, e.g. not field by field but by serializing and…
user3735658
0
votes
1 answer

How do I access a constexpr created type as another type while staying constexpr? (using with static_assert)

I've been trying to figure this one out, and thought it would be a fun one to take a look at :) Ok, so I'm creating a type as constexpr using bitfields. Since bitfields can change from one implementation or platform to another, I want to…
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
0
votes
2 answers

Concise bit-manipulation for 64bit integer handle type

I have a 64bit integer that is used as a handle. The 64bits must be sliced into the following fields, to be accessed individually: size : 30 bits offset : 30 bits invalid flag : 1 bit immutable flag : 1 bit type flag : 1…
glampert
  • 4,371
  • 2
  • 23
  • 49
0
votes
1 answer

Using Javascript (not JQuery) to tally a range of checkboxes,

I'm currently working on a php script that will be used to perform a search query on a database. (Below is the form, coding and appearance, the backend is ZenCart 1.5.1 (not that it should matter))
Paul Williams
  • 1,554
  • 7
  • 40
  • 75
0
votes
1 answer

C++ - Defining 1 Bit Bools

What are the consequences of defining a structure as follows: typedef struct { bool Bit0 : 1; //Bit 0 bool Bit1 : 1; bool Bit2 : 1; bool Bit3 : 1; bool Bit4 : 1; bool …
ALM865
  • 1,078
  • 13
  • 21
0
votes
2 answers

Misalignment of members in structures

In C, sometimes certain members of a structure tend to have misaligned offsets, as in case of this thread in HPUX community In such a case, one is suggested to use zero-width bit field to align the(misaligned) next member. Under what circumstance…
Phalgun
  • 1,181
  • 2
  • 15
  • 42
0
votes
2 answers

BitField Variable Syntax

I am interested in the syntax convention for BitFields in C++ and if different methods of naming variables need to be accounted for in the number of allocated bits. union BitField32 { struct { unsigned int a : 1; unsigned int b :…
Haydn Trigg
  • 105
  • 1
  • 6
0
votes
1 answer

Signed bitfields require at least two bits

I have the following struct: struct holder { int cause; int agent:1, group:1, supervisor:1, defer:1; }; Klocwork complains at the int agent:1 ... line saying: Bit field 'agent' has one bit and is signed. Signed one bit field,…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
0
votes
2 answers

malloc bit field values to array in c

what i am trying to accomplish is user enters bit field widths, say 17 5 8 19 0 (can be more or less bit fields) 0 states end of bit field inputs then user enters in values to be stored in a allocated array set to the bit field sizes. say 1 2 3 4…