C/C++ bitfields versus bitwise operators to single out bits, which is faster, better, more portable? -


I have to pack some bits into byte in this byte:

  struct {char} Bit 0: 1; Four bit 1: 1; } a; If (a.bit1) / * etc * /  

or:

  if (a & amp; 0x2) / * etc * /  

It is very clear from the source code that bitfield knit is but the option is fast? I know that the speed difference will not be too much if any, but as I can use any of them, if one is fast, then better. On the other hand, I have read that BitField does not guarantee to arrange bits, they want to order the same platforms, and I want to make my code portable.

Note: If you want to reply to 'profile', I will, but as I am lazy, if someone already answers, very good. The code can be wrong, you can correct me if you want, but remember what this question means and please try and answer it too.

I will use the second instance in priority for maximum portability. As Neil Butterworth pointed out, BitField is only used for the original processor. Okay, think about this, what happens if Intel's X86 is out of business tomorrow, the code will get stuck, which means that the second processor has to re-apply BitField, the RISC chip is called.

You have to see the big picture and ask how the Open BSD has stopped its BSD system in many platforms using a codebase? Okay, I agree that it is a bit at the top, and is debatable and subjective, but in reality, if you want to turn off the code in any other platform, then this is another example you used in your question The way to do this is by using.

Not alone, compilers have their own way of padding for different platforms, aligning bitfields for processors where the compiler is running. And besides, what about the processor's endlessness?

Never trust a bit bullets as a magic bullet. If you want speed for the processor and will be fixed on it, i.e. there is no intention of porting, feel free to use bitfield. You can not have both!


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -