bit-fields

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

南笙酒味 提交于 2019-11-27 04:44:58
问题 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? 回答1: 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

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

折月煮酒 提交于 2019-11-27 01:47:59
问题 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? 回答1: It's an often-asked question. There's an ASPN Cookbook entry on it that has served me in the past. And there

C++ bitfield packing with bools

情到浓时终转凉″ 提交于 2019-11-26 22:48:32
问题 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; };

struct bitfield max size (C99, C++)

做~自己de王妃 提交于 2019-11-26 20:57:03
问题 What is maximal bit width for bit struct field? struct i { long long i:127;} Can I define a bit field inside struct, with size of bitfield up to 128 bit, or 256 bit, or larger? There are some extra-wide vector types, like sse2 (128-bit), avx1/avx2 (256-bit), avx-512 (512-bit for next Xeon Phis) registers; and also extensions like __int128 in gcc. 回答1: C99 §6.7.2.1, paragraph 3: The expression that specifies the width of a bit-field shall be an integer constant expression that has nonnegative

Bit-fields of type other than int?

送分小仙女□ 提交于 2019-11-26 18:28:29
问题 I have a code which uses bit-fields declared as follows typedef struct my{ const char *name; uint8_t is_alpha : 1; uint8_t is_hwaccel : 1; uint8_t x_chroma_shift; uint8_t y_chroma_shift; } mystr; uint8_t is typedef'ed to unsigned char . Building the code in MS-VS 2008 using this bit fields gives a warning as below: imgconvert.c(60) : warning C4214: nonstandard extension used : bit-field types other than int. Is there any problems/potential issues in using bit fields of type other than int?

Converting Bit Field to int

核能气质少年 提交于 2019-11-26 16:44:01
问题 I have bit field declared this way: typedef struct morder { unsigned int targetRegister : 3; unsigned int targetMethodOfAddressing : 3; unsigned int originRegister : 3; unsigned int originMethodOfAddressing : 3; unsigned int oCode : 4; } bitset; I also have int array, and i want to get int value from this array, that represents the actual value of this bit field (which is actually some kind of machine word that i have the parts of it, and i want the int representation of the whole word).

Does Python have a bitfield type?

别来无恙 提交于 2019-11-26 16:07:57
I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution? Bitarray was the best answer I found, when I recently had a similar need. It's a C extension (so much faster than BitVector, which is pure python) and stores its data in an actual bitfield (so it's eight times more memory efficient than a numpy boolean array, which appears to use a byte per element.) nealmcb If you mainly want to be able to name your bit fields and easily manipulate them, e.g. to work with flags represented as single bits in a

Order of fields when using a bit field in C

心已入冬 提交于 2019-11-26 11:32:24
问题 I have a struct of the following type typedef struct { unsigned int a : 8; unsigned int b : 6; unsigned int c : 2; }x, *ptr; What i would like to do, is change the value of field c. I do something like the following x structure = { 0 }; x->c = 1; When I look at the memory map, I expect to find 00 01 , but instead I find 00 40 . It looks like when arranging the second byte, it puts c field in the lowest bits and b field in the highest bits. I\'ve seen this on both GCC and Windows compilers.

Bit fields in C#

瘦欲@ 提交于 2019-11-26 10:19:45
I have a structure which I need to populate and write to disk (several actually). An example is: byte-6 bit0 - original_or_copy bit1 - copyright bit2 - data_alignment_indicator bit3 - PES_priority bit4-bit5 - PES_scrambling control. bit6-bit7 - reserved In C I might do something like the following: struct PESHeader { unsigned reserved:2; unsigned scrambling_control:2; unsigned priority:1; unsigned data_alignment_indicator:1; unsigned copyright:1; unsigned original_or_copy:1; }; Is there any way to do this in C# that would enable me to access the bits using the struct dereferencing dot operator

Is it safe to use -1 to set all bits to true?

早过忘川 提交于 2019-11-26 08:50:14
问题 I\'ve seen this pattern used a lot in C & C++. unsigned int flags = -1; // all bits are true Is this a good portable way to accomplish this? Or is using 0xffffffff or ~0 better? 回答1: I recommend you to do it exactly as you have shown, since it is the most straight forward one. Initialize to -1 which will work always , independent of the actual sign representation, while ~ will sometimes have surprising behavior because you will have to have the right operand type. Only then you will get the