bit-fields

Practical Use of Zero-Length Bitfields

不想你离开。 提交于 2019-11-26 06:07:05
问题 I am not totally sure about C, but C++ allows unnamed bit-fields of 0 length. For example: struct X { int : 0; }; Question one: What practical uses of this can you think of? Question two: What real-world practical uses (if any) are you aware of? Edited the example after ice-crime\'s answer Edit: OK, thanks to the current answers I now know the theoretical purpose. But the questions are about practical uses so they still hold :) 回答1: You use a zero-length bitfield as a hacky way to get your

Bit fields in C#

旧街凉风 提交于 2019-11-26 05:51:24
问题 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

Does Python have a bitfield type?

旧巷老猫 提交于 2019-11-26 04:43:16
问题 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? 回答1: 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.) 回答2: If you mainly want to be able to name

What does 'unsigned temp:3' in a struct or union mean? [duplicate]

感情迁移 提交于 2019-11-26 00:09:57
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What does this C++ code mean? I\'m trying to map a C structure to Java using JNA. I came across something that I\'ve never seen. The struct definition is as follows: struct op { unsigned op_type:9; //---> what does this mean? unsigned op_opt:1; unsigned op_latefree:1; unsigned op_latefreed:1; unsigned op_attached:1; unsigned op_spare:3; U8 op_flags; U8 op_private; }; You can see some variable being defined like

Why bit endianness is an issue in bitfields?

♀尐吖头ヾ 提交于 2019-11-25 23:59:02
问题 Any portable code that uses bitfields seems to distinguish between little- and big-endian platforms. See the declaration of struct iphdr in linux kernel for an example of such code. I fail to understand why bit endianness is an issue at all. As far as I understand, bitfields are purely compiler constructs, used to facilitate bit level manipulations. For instance, consider the following bitfield: struct ParsedInt { unsigned int f1:1; unsigned int f2:3; unsigned int f3:4; }; uint8_t i; struct

What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?

纵饮孤独 提交于 2019-11-25 22:43:12
问题 What does the following C++ code mean? unsigned char a : 1; unsigned char b : 7; I guess it creates two char a and b, and both of them should be one byte long, but I have no idea what the \": 1\" and \": 7\" part does. 回答1: The 1 and the 7 are bit sizes to limit the range of the values. They're typically found in structures and unions. For example, on some systems (depends on char width and packing rules, etc), the code: typedef struct { unsigned char a : 1; unsigned char b : 7; }