bit access on value types.
Posted: Thu Oct 04, 2007 8:15 am
I have written a template class to manipulate bits in a value type and now i am interested in how many people would find it a use full feature/template. The template does take some performance issues into account so it is still fast.
Consider the following program fragment:
As the commentary already explains it should set the value type data to 0xF6. The example shows both the use of the bitRange function and the operator () equivalent. Below shows the assembly output of these C++ statements.
GCC nicely optimizes this to one single instruction.
I am thinking of making a template class for value types, for instance int8, int16, int32, int64 and there unsigned equivalents were these bit manipulation function are incorporated.
Usefull or not.
Consider the following program fragment:
Code: Select all
static BitField<int8u> data;
data.bitRange(0, 4) = 0; //- clear bits 0..3 (4 bits) (data=0x00)
data(4, 4) = -1; //- set bits 4..7 (4 bits) to -1 (data=0xF0)
data(1, 2) = -1; //- set bits 1..2 (2 bits) to -1 (data=0xF6)
Code: Select all
c6 05 00 00 00 00 f6 movb $0xf6,0(%rip)
I am thinking of making a template class for value types, for instance int8, int16, int32, int64 and there unsigned equivalents were these bit manipulation function are incorporated.
Usefull or not.