bit extraction from BYTE
-
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
-
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
-
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
-
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
field1 = (data & 0x0F);
field2 = (data & 0x10) >> 4;
field3 = (data & 0xE0) >> 5;«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
field1 = (data & 0x0F);
field2 = (data & 0x10) >> 4;
field3 = (data & 0xE0) >> 5;«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)A bit more optimization in field3
«_Superman_» wrote:
field3 = (data & 0xE0) >> 5;
field3 = data>> 5;
-
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
The two different ways (you may find they are translated to the same asm, by the compiler). [update] the first method's asm (SHIFT and AND operator) looks slightly faster)[/update]
struct BitField
{
unsigned int f1:4;
unsigned int f2:1;
unsigned int f3:3;
};union MyData
{
unsigned char data;
BitField bf;
};int main(int argc, char *argv[])
{
unsigned char data= rand();int field1, field2, field3;
// using SHIFT and AND operators
field1 = data & 0xF;
field2 = (data >> 4) & 1;
field3 = data >> 5;printf("data=%d\n", data);
printf("(1) fields: %d, %d, %d\n", field1, field2, field3);// using union and bitfield struct
MyData md;
md.data = data;field1 = md.bf.f1;
field2 = md.bf.f2;
field3 = md.bf.f3;
printf("(2) fields: %d, %d, %d\n", field1, field2, field3);
return 0;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Wednesday, March 10, 2010 5:31 AM
-
A bit more optimization in field3
«_Superman_» wrote:
field3 = (data & 0xE0) >> 5;
field3 = data>> 5;
Perfect. :thumbsup:
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance
-
field 1.... bit 0-3 = > (data & 3) and so on
Press F1 for help or google it. Greetings from Germany
KarstenK wrote:
field 1.... bit 0-3 = > (data & 3)
field 1.... bit 0-3 = > (data & 15) FFY. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]