|
Hi,
That is an excellent link to an awesome collection of bit manipulation codes. I have used many of them in various projects over the years.
Best Wishes,
-David Delaune
|
|
|
|
|
|
Inefficient, sure, but maybe easier to grasp.
BYTE b = 139;
BYTE r = 0;
for (int i=0; i<8; i++)
{
r = r << 1;
r |= b & 0x1;
b = b >> 1;
}
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'm not sure it's much less efficient - all those multiplies will quite a high cost.
If it's just a BYTE, you could cut it down more by not even bothering with a loop:
BYTE b = 139;
BYTE r;
r = ((b & 1) << 7) | ((b & 2) << 5) | ((b & 4) << 3) | ((b & 8) << 1) | ((b & 16) >> 1) | ((b & 32) >> 3) | ((b & 64) << 5) | ((b & 128) >> 7) )
This would not extend well for unsigned __int64 ...
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Uhm...loop unrolling: the oldie-goldie game-hacker Iain!
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]
|
|
|
|
|
Your version should be slightly faster. In theory on the Pentium 4 instruction cycle count should be as follows:
Bit masking technique:
3 MUL * 16 cycles
1 OR * 0.5 cycle
2 AND * 0.5 cycle
1 SHR * 4 cycles
===============
53.5 cycles
Your unrolled shifting:
8 AND * 0.5 cycles
7 OR * 0.5 cycles
5 SHL * 4 cycles
3 SHR * 4 cycles
================
39.5
It would be interesting to have MSVC output an .asm file with the /FA option. It would be interesting to see what the 2005/2008 compiler optimizer does to all of our codes!
Best Wishes,
-David Delaune
|
|
|
|
|
I think you may drop off a pair of AND s in Iain expression.
BTW: very good job, I like soo much your posts.
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]
|
|
|
|
|
Hi,
if you need it once, the answers given to you are fine.
if you need to bit-swap an entire file or large memory buffer, I suggest you create a 256-byte lookup table, which you initialize by running one of the given code snippets for all possible byte values.
|
|
|
|
|
hi, everyone
recently I started a new project which needs to be written in plain C,
and I got problem with different compilers that:
I want the declaration of public APIs along with data types required to
be in some separate files, and the definition of the types in some others,
for example:
interface.h
struct _VECTOR;
/**
* there's a problem with the absence of 'struct' in function
* declaration at least in mingw2.95, but that's not the point
* of my post.
*/
void _InitVector(struct _VECTOR* pVector);
implementation.h
#include "interface.h"
/* my point is here */
typedef struct _tagVECTOR {
int a;
} _VECTOR, *_PVECTOR;
implementation.c
#include "implementation.h"
void _InitVector(struct _VECTOR* pVector)
{
pVector->a = 1; /* derefferencing pointer to imcomplete type. */
}
It seems that the compiler cannot recognize the fact that _VECTOR is a typedef
name of the struct _tagVECTOR;
By the definition of 'typedef', I know that _VECTOR declared in "interface.h"
is exactly the same entity to that in "implementation.h", although it turned
out to be a typedef name of _tagVECTOR, but it won't compile in mingw2.95 with
the error mentioned in the code above.
Revised version can solve the problem:
typedef struct _VECTOR {
int a;
} *_PVECTOR;
But I want it to be consistent with the general idea, so I revised further:
typedef struct _VECTOR {
int a;
} _VECTOR, *_PVECTOR;
It compiled with no problem, but I'm not sure if I'm lucky enough with other
compilers holding the potential issue of "typedef redefinition".
Thanks in advance.
|
|
|
|
|
Why are you posting a plain C question in a C++ / MFC forum?
That said, I think you should write
void _InitVector(_VECTOR* pVector)
instead of
void _InitVector(struct _VECTOR* pVector)
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
hi,
The joke is that because I think C is a subset of C++.
And I got problem with the absence of that 'struct':
[Error] parse error before '*'
It seems the pre-declaration can never be sighted by the compiler(mingw2.95).
|
|
|
|
|
6Qing88 wrote: And I got problem with the absence of that 'struct
Sorry I didn't read your post carefully. The problem is that you declare _VECTOR as a struct first
struct _VECTOR;
void _InitVector(struct _VECTOR* pVector);
but as a typedef later
typedef struct _tagVECTOR {
int a;
} _VECTOR, *_PVECTOR;
You can't forward declare a typedef as a struct, because it is not a struct - it is a typedef. I think it is intended to be that way (i.e. it's not the compiler but the C language specs that require it).
You can try writing this at the header (I haven't tested it)
struct _tagVECTOR;
typedef struct _tagVECTOR _VECTOR;
void _InitVector(_VECTOR * pVector);
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
Your problem is that you're using struct _VECTOR in interface.h and _VECTOR == struct _tagVECTOR elsewhere. Those are two different types. It doesn't matter that you use _VECTOR in both - struct tag names are in a different namespace than type names.
Thus, in implementation.c , struct _VECTOR is incomplete. struct _tagVECTOR , aka _VECTOR is complete...but you are not referencing that type!
|
|
|
|
|
Hi all,
I am making look and feel of a Dialog same as message box can anybody tell me what font and size should i make it so that it should look exactly similar as messageBox.
Thanks
vikas da
|
|
|
|
|
You should use the MS Shell Dlg or MS Shell Dlg [^] logical fonts to have the same behavior of MS Windows dialogs. I am pretty sure that the default font size is 8 for most english users but if I remember correctly the default font size can be larger in an international installation.
Also keep in mind that although most users with 20-20 vision have the DPI set at 96, there may be individuals with DPI set at 120. You should test your messagebox dialog to ensure that it can handle all environments.
Best Wishes,
-David Delaune
|
|
|
|
|
guide me load data to combobox for vc++6.0. please. thanks
"
enum { IDD = IDD_SPOOLIMAGE_DS_PP };//gọi form
DDX_Control(pDX, IDC_TEST, value_del);
CComboBox* cb = (CComboBox*)GetDlgItem(IDC_TEST);
cb->Clear();
CString sName = "";
for(int i=0; i<10;i++)
{
cb->SetItemData(i, DWORD(i));
}
"
|
|
|
|
|
And what the problem?
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
can you tell specification ? thanks . wish your help
|
|
|
|
|
If you want to add strings to Combobox you can use of:
for (int i = 0; i < m_nItems; i++)
{
m_pComboBox->InsertString(str);
}
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
why don't you use to AddString() to load data into the combo box?
|
|
|
|
|
I was Addstring() but wrong.
|
|
|
|
|
nguyen phan wrote: cb->SetItemData(i, DWORD(i));
SetItemData() is used to Set the 32-bit value associated with the specified item in a combo box. AddString() is used to add a string to the end of the list in the list box of a combo box.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
How to Invoke a Web Service using managed C++ OR VC++ .NET . provide me with some link
|
|
|
|
|
|
Did you see Web Services[^]?
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|