|
|
Read up on unions and structures - that's the elegant way to do it.
Bitwise-and and right-shifting (suggested by the previous poster) will also do the job nicely.
|
|
|
|
|
field1 = (data & 0x0F);
field2 = (data & 0x10) >> 4;
field3 = (data & 0xE0) >> 5;
|
|
|
|
|
A bit more optimization in field3
«_Superman_» wrote: field3 = (data & 0xE0) >> 5;
field3 = data>> 5;
|
|
|
|
|
Perfect.
|
|
|
|
|
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;
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);
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
|
|
|
|
|
field 1.... bit 0-3 = > (data & 3)
and so onPress 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]
|
|
|
|
|
I have created 3 modeless child dialogbox dynamically on Parent dialog,
CDialogBoxDlg *m_Sel[3];
for(int i=0;i<3;i++)
{
m_Sel[i]=new CDialogBoxDlg();
m_Sel[i]->Create (IDD_DIALOG,this);
m_Sel[i]->ShowWindow (SW_SHOW);
}
after creating ,if i click on title bar and move the dialog1 or dialog2 or dialog 3,then it will not look clearly,if i Resize the Border then it will looks clearly.
i tried Invalidate(), UpdateWindow() and RedrawWindow() then also it is not working please help.
-- Modified Wednesday, March 10, 2010 4:12 AM
|
|
|
|
|
Did you call UpdateWindow in this way
for(int i=0;i<3;i++)
{
m_Sel[i]=new CDialogBoxDlg();
m_Sel[i]->Create (IDD_DIALOG,this);
m_Sel[i]->ShowWindow (SW_SHOW);
m_Sel[i]->UpdateWindow();
} modified on Wednesday, March 10, 2010 3:59 AM
|
|
|
|
|
Also, you need to have an array of 3 locations - CDialogBoxDlg *m_Sel[3];
The valid locations are now from 0 to 2
|
|
|
|
|
I am creating a blocking API implementation. For that i am using a semaphore which is initialized once in the beginning of the program. A pseudo code of the scenario is given below:
startProgram()
{
initCount = 0; //first call shall wait
semVariable = CreateSemaphore (NULL, initCount, 1, NULL);
}
I am calling the semaphore in my API:
API_my_blocking_func()//thread1
{
//Wait until data is ready in do_unblock()
if(WAIT_TIMEOUT == WaitForSingleObject (semVariable, timeout)
{
return WAIT_TIMEOUT ;
}
continues........
}
meanwhile in another thread, when data is ready, I am releasing semaphore so that API_my_blocking_func() continues.
do_unblock() //trhead2
{
if(data is ready)
ReleaseSemaphore (semVariable, 1, NULL);
}
----
Now I am having a problem : If ReleaseSemaphore (with semVariable) is called two times due to some reason,
the WaitForSingleObject is not waiting!
To my expectation, since the maximum count is set to 1 , releasing the semaphore
shall not increment semVariable above 1, whatever be the number of calls to ReleaseSemaphore . So the very next
WaitForSingleObject shall block.
How to achieve multiple release of semaphore is allowed, and a single WaitForSingleObject on the same semaphore shall block?
|
|
|
|
|
its not clear what do you need. whenever the semaphore is released its count is increased and a WaitForSingleObject() will be unblocked. If you need to wait till a set of flags are signaled, use array of events or mutexes and use WaitForMultipleObjects()
|
|
|
|
|
Since 'maximum count is set to 1' during semaphore initialization, when semaphore is released its count is increased to 1 only. Not beyond (is this true?). So n number of releases shall keep maximum count 1. And next wait shall decrement by 1 and thus block.
What I observed is : If i release same semaphore multiple times, I cannot wait on that semaphore in a single wait call.
|
|
|
|
|
|
Hello All,
I am registering an ActiveX as follows:
CString ocxFile = L" /s \"" + ocxFile + L"\"";
HINSTANCE hInstance = ::ShellExecute(NULL, L"open", L"regsvr32.exe", ocxFile, NULL, SW_HIDE);
It works fine when user has admin rights . If user does not have admin rights then it can not register. Any solution of it ?
I am developing in XP and testing in xp and win7
Thanks,
Rahul
|
|
|
|
|
Registering an OCX is actually creating a few registry entries.
This requires admin rights by default unless the permission for the registry keys have been changed.
This is a good security feature.
|
|
|
|
|
Any way to give those permissions like showing UAC which will ask for an admin password.
If UAC is the solution then how to show UAC ?
Thanks,
Rahul
|
|
|
|
|
Take a look at the link I've provided to you in my other post. You need to specify this in the manifest file.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thanks Rajesh!
I am looking into it.
|
|
|
|
|
Go to Project Properties -> Configuration Properties -> Linker -> Manifest File .
Change UAC Execution Level entry to requireAdministrator .
|
|
|
|
|
Thanks Superman & Rajesh,
It worked but my applicaiton stores some data in applicationData folder.
So if I use UAC then it uses applicaitonData folder of administrator account.
There should be some way to use it's own application folder.
I want this because my application is user specific so I use application data folder as a storage place. But by using UAC normal user's data is getting store inside administrators application data.
To retrieve application data folder I am using
SHGetSpecialFolderPath(0, strPath, CSIDL_APPDATA, FALSE);
which is giving admin's aplication data folder after using UAC.
|
|
|
|
|
I wish there should be some way to mark more than one answer as a Solution.
Your answer also deserve to be selected as "Solution". Sadly, I could not
|
|
|
|
|
You cannot execute this without running it as an administrator. Your application needs administrator privileges[^].
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Is there any way to change UAC only for few statements of code and after those statements, application will run in normal UAC.
|
|
|
|