Click here to Skip to main content
15,889,651 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionbit extraction from BYTE Pin
hrishi3219-Mar-10 22:19
hrishi3219-Mar-10 22:19 
AnswerMessage Removed Pin
9-Mar-10 22:26
Cool_Dev9-Mar-10 22:26 
GeneralRe: bit extraction from BYTE Pin
KarstenK18-Mar-10 22:15
mveKarstenK18-Mar-10 22:15 
AnswerRe: bit extraction from BYTE Pin
normanS9-Mar-10 22:37
normanS9-Mar-10 22:37 
AnswerRe: bit extraction from BYTE Pin
«_Superman_»9-Mar-10 22:56
professional«_Superman_»9-Mar-10 22:56 
GeneralRe: bit extraction from BYTE Pin
Mohan Ramachandra9-Mar-10 23:09
Mohan Ramachandra9-Mar-10 23:09 
GeneralRe: bit extraction from BYTE Pin
«_Superman_»9-Mar-10 23:21
professional«_Superman_»9-Mar-10 23:21 
AnswerRe: bit extraction from BYTE Pin
CPallini9-Mar-10 23:18
mveCPallini9-Mar-10 23:18 
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;
}


Smile | :)
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

AnswerRe: bit extraction from BYTE Pin
KarstenK10-Mar-10 4:15
mveKarstenK10-Mar-10 4:15 
GeneralRe: bit extraction from BYTE Pin
CPallini10-Mar-10 11:24
mveCPallini10-Mar-10 11:24 
QuestionDialog Box Resize [modified] Pin
shiv@nand9-Mar-10 21:48
shiv@nand9-Mar-10 21:48 
AnswerRe: Dialog Box Resize Pin
Mohan Ramachandra9-Mar-10 21:53
Mohan Ramachandra9-Mar-10 21:53 
AnswerRe: Dialog Box Resize Pin
«_Superman_»9-Mar-10 22:10
professional«_Superman_»9-Mar-10 22:10 
QuestionProblem with WaitForSingleObject for multiple calls to ReleaseSemaphore Pin
temp.cyberfiles9-Mar-10 21:34
temp.cyberfiles9-Mar-10 21:34 
AnswerRe: Problem with WaitForSingleObject for multiple calls to ReleaseSemaphore Pin
Cool_Dev9-Mar-10 22:09
Cool_Dev9-Mar-10 22:09 
GeneralRe: Problem with WaitForSingleObject for multiple calls to ReleaseSemaphore Pin
temp.cyberfiles9-Mar-10 22:26
temp.cyberfiles9-Mar-10 22:26 
GeneralRe: Problem with WaitForSingleObject for multiple calls to ReleaseSemaphore Pin
Cool_Dev9-Mar-10 22:29
Cool_Dev9-Mar-10 22:29 
QuestionHow to register an ActiveX ocx file Pin
rahul.kulshreshtha9-Mar-10 20:16
rahul.kulshreshtha9-Mar-10 20:16 
AnswerRe: How to register an ActiveX ocx file Pin
«_Superman_»9-Mar-10 20:24
professional«_Superman_»9-Mar-10 20:24 
GeneralRe: How to register an ActiveX ocx file Pin
rahul.kulshreshtha9-Mar-10 20:26
rahul.kulshreshtha9-Mar-10 20:26 
GeneralRe: How to register an ActiveX ocx file Pin
Rajesh R Subramanian9-Mar-10 20:28
professionalRajesh R Subramanian9-Mar-10 20:28 
GeneralRe: How to register an ActiveX ocx file Pin
rahul.kulshreshtha9-Mar-10 20:29
rahul.kulshreshtha9-Mar-10 20:29 
GeneralRe: How to register an ActiveX ocx file Pin
«_Superman_»9-Mar-10 20:28
professional«_Superman_»9-Mar-10 20:28 
GeneralRe: How to register an ActiveX ocx file Pin
rahul.kulshreshtha9-Mar-10 21:05
rahul.kulshreshtha9-Mar-10 21:05 
GeneralRe: How to register an ActiveX ocx file Pin
rahul.kulshreshtha9-Mar-10 21:22
rahul.kulshreshtha9-Mar-10 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.