bit-fields

C++ 2-bit bitfield arrays possible?

拥有回忆 提交于 2019-11-28 08:56:53
问题 I have a struct of 2-bit bitfields like this: struct MyStruct { unsigned __int32 info0 : 2; unsigned __int32 info1 : 2; unsigned __int32 info2 : 2; ... unsigned __int32 info59 : 2; }; And another like this going up to 120... Is there a way to write and address them as an array? 回答1: If you can't use Paul R's answer for whatever reason, you can always use a custom accessor with a standard array : static unsigned __int8 infos[30]; // 240 bits allocated unsigned __int8 getInfo( unsigned short id

What is the best way to do Bit Field manipulation in Python?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 07:20:53
I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the way C does bit fields (without having to revert to C). Suggestions? It's an often-asked question. There's an ASPN Cookbook entry on it that has served me in the past. And there is an extensive page of requirements one person would like to see from a module doing this. The bitstring

Why and how are C++ bitfields non-portable?

自古美人都是妖i 提交于 2019-11-28 02:53:49
问题 I've come across many comments on various questions regarding bitfields asserting that bitfields are non-portable, but I've never been able to find a source explaining precisely why. At face value, I would have presumed all bitfields merely compile to variations of the same bitshifting code, but evidently there must be more too it than that or there would not be such vehement dislike for them. So my question is what is it that makes bitfields non-portable? 回答1: Bit fields are non-portable in

Bit-fields “In-class initialization” results in “error: lvalue required as left operand of assignment”

痴心易碎 提交于 2019-11-28 00:46:08
struct bitfield { int i = 0; // ok int j : 8 = 0; // error: lvalue required as left operand of assignment }; What is the correct syntax to initialize bit-fields using C++11 "in-class initialization" feature? What is the correct syntax to initialize bit-fields using C++11 "in-class initialization" feature? You cannot initialize bit-fields in-class. Paragraph 9.2 of the C++11 Standard specifies the grammar for class member declarators: [...] member-declarator: declarator virt-specifier-seq(opt) pure-specifier(opt) declarator brace-or-equal-initializer(opt) identifier(opt) attribute-specifier-seq

Memory layout of struct having bitfields

为君一笑 提交于 2019-11-28 00:04:08
I have this C struct: (representing an IP datagram) struct ip_dgram { unsigned int ver : 4; unsigned int hlen : 4; unsigned int stype : 8; unsigned int tlen : 16; unsigned int fid : 16; unsigned int flags : 3; unsigned int foff : 13; unsigned int ttl : 8; unsigned int pcol : 8; unsigned int chksm : 16; unsigned int src : 32; unsigned int des : 32; unsigned char opt[40]; }; I'm assigning values to it, and then printing its memory layout in 16-bit words like this: //prints 16 bits at a time void print_dgram(struct ip_dgram dgram) { unsigned short int* ptr = (unsigned short int*)&dgram; int i,j;

When is it worthwhile to use bit fields?

一笑奈何 提交于 2019-11-27 19:58:43
Is it worthwhile using C's bit-field implementation? If so, when is it ever used? I was looking through some emulator code and it looks like the registers for the chips are not being implemented using bit fields. Is this something that is avoided for performance reasons (or some other reason)? Are there still times when bit-fields are used? (ie firmware to put on actual chips, etc) Bit-fields are typically only used when there's a need to map structure fields to specific bit slices, where some hardware will be interpreting the raw bits. An example might be assembling an IP packet header. I can

C++ bitfield packing with bools

筅森魡賤 提交于 2019-11-27 19:20:40
I've just done a test with bitfields, and the results are surprising me. class test1 { public: bool test_a:1; bool test_b:1; bool test_c:1; bool test_d:1; bool test_e:1; bool test_f:1; bool test_g:1; bool test_h:1; }; class test2 { public: int test_a:1; int test_b:1; int test_c:1; int test_d:1; int test_e:1; int test_f:1; int test_g:1; int test_h:1; }; class test3 { public: int test_a:1; bool test_b:1; int test_c:1; bool test_d:1; int test_e:1; bool test_f:1; int test_g:1; bool test_h:1; }; The results were:- sizeof(test1) = 1 // This is what I'd expect. 8 bits in a byte sizeof(test2) = 4 //

How to simulate bit-fields in Delphi records?

守給你的承諾、 提交于 2019-11-27 18:10:57
I would like to declare a record in Delphi that contains the same layout as it has in C. For those interested : This record is part of a union in the Windows OS's LDT_ENTRY record. (I need to use this record in Delphi because I'm working on an Xbox emulator in Delphi - see project Dxbx on sourceforge). Anyway, the record in question is defined as: struct { DWORD BaseMid : 8; DWORD Type : 5; DWORD Dpl : 2; DWORD Pres : 1; DWORD LimitHi : 4; DWORD Sys : 1; DWORD Reserved_0 : 1; DWORD Default_Big : 1; DWORD Granularity : 1; DWORD BaseHi : 8; } Bits; As far as I know, there are no bit-fields

Marshalling stucts with bit-fields in C#

我的梦境 提交于 2019-11-27 16:57:42
问题 Is it possible to marshal a C-style struct containing bit-fields to a C# struct, or would you have to marshal it to a basic type and then do bit-masks? E.g. I would like to marshal from a C style structure like this: struct rgb16 { unsigned int R : 4; unsigned int G : 5; unsigned int B : 4; } And marshal it onto something like this: [StructLayout(LayoutKind.Sequential)] public struct Rgb16 { public byte R; public byte G; public byte B; } 回答1: There are no bit-fields in C#. So I'd go with

Questions about C bitfields

独自空忆成欢 提交于 2019-11-27 15:38:40
Is bitfield a C concept or C++? Can it be used only within a structure? What are the other places we can use them? AFAIK, bitfields are special structure variables that occupy the memory only for specified no. of bits. It is useful in saving memory and nothing else. Am I correct? I coded a small program to understand the usage of bitfields - But, I think it is not working as expected. I expect the size of the below structure to be 1+4+2 = 7 bytes (considering the size of unsigned int is 4 bytes on my machine), But to my surprise it turns out to be 12 bytes (4+4+4). Can anyone let me know why?