Is a bit field any more efficient (computationally) than masking bits and extracting the data by hand?

后端 未结 7 1655
庸人自扰
庸人自扰 2021-02-08 10:16

I have a numerous small pieces of data that I want to be able to shove into one larger data type. Let\'s say that, hypothetically, this is a date and time. The obvious method is

7条回答
  •  粉色の甜心
    2021-02-08 10:50

    The compiler generates the same instructions that you would explicitly write to access the bits. So don't expect it to be faster with bitfields.

    In fact, strictly speaking with bitfields you don't control how they are positioned in the word of data (unless your compiler gives you some additional guarantees. I mean that the C99 standard doesn't define any). Doing masks by hand, you can at least place the two most often accessed fields first and last in the series, because in these two positions, it takes one operation instead of two to isolate the field.

提交回复
热议问题