|
There was the C language , a true miracle of conciseness...But it was a long, long time ago
I agree on generality, a bit less on readability
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.
|
|
|
|
|
Are you serious?
Compare
if(IsBitSet(flags, 5)) ... to
ASSERT(some check that flags is big enough for 1 to be shifted 4 steps to the left);
if(flags & (1 << 4)) ... Which one do you think conveys most meaning?
I don't for a second distrust your ability to understand what the second one does, syntactically, but the first one has a level of semantics that the second lacks completely, and that alone is A Good Thing™.
--
Time you enjoy wasting is not wasted time - Bertrand Russel
|
|
|
|
|
Johann Gerell wrote: Are you serious?
Of course.
Knowing a bit of bits ( ) then you have to admit that semantics has to be found also in the second expression.
Working at bit level may be done with conciseness and elegance: Why making complex what may be simple?
(just my point of view, of course...)
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.
|
|
|
|
|
I use a lot of bitflags, and my preference is to use enums as
Johann Gerell wrote: if(IsBitSet(flags, 5)) ...
whilst readable does not indicate what bit 5 is. I prefer to define in the appropriate namespace
<br />
enum {FLAG1 = 0x01, FLAG2= 0x02, FLAG3 = 0x04, FLAG4 = 0x08, FLAG5 = 0x10 }; etc<br />
with informative names for FLAG1 etc.
then test flags as
if (flags & FLAG2)
I also often include an inline function
void ModifyFlags(DWORD add, DWORD remove) {flags = (flags & ~remove)|add;}
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
#define IsBitSet(_val,_bit) (((_val) & (1 << (_bit)))==(1 << (_bit)))
|
|
|
|
|
paper67 wrote: Does anybody know a macro to check if only one bit is set in a word value ?
... if on the other hand you are asking how to check if only one bit is set in a word value (not the state of a given bit) ...
If only one bit is set then the value will be a power of 2.
To check if a value is a power of 2 you can use:
#define ISPOW2(x) ( ((x)&((x)-1)) == 0 )
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Hi,
I forgot to mention that I do not know the position of the bit within the WORD value.
So, I want to check a WORD if it contains a one bit value (bit flag) without knowing its position.
Thx.
|
|
|
|
|
The following counts how many bits are set.
INT howManyBits(WORD w)
{
WORD wSingleBit=1;
INT iCount=0;
while (wSingleBit)
{
if ( wSingleBit & w ) iCount++;
wSingleBit <<= 1;
}
return iCount;
}
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.
|
|
|
|
|
Hi:
I am building a client program in VC++ 6.0 using WINSOCK to connect to the server. I have a very small program that testing WINSOCK. My program contains include statement for WINSOCK2.h. Here is the code to initialize WINSOCK.
WSADATA wsaData;
int iResult;
iResult = WSAStartup(0x0202, &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
}
Errors:
Linking...
testwin1Dlg.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/testwin1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
testwin1.exe - 2 error(s), 0 warning(s)
Additional Comment:
I looked in WINSOCK.H and WINSOCK2.H and both has WSAStartup. In WINSOCK.H, it was PASCAL version. In WINSOCK2.H, it was either C or C++ version. Both version has the same signature (calling argurment).
Can this be an issue? if yes, how can I eliminate the PASCAL version (WINSOCK.H)? Note that, I never include WINSOCK.H.
Thanks
Bill
|
|
|
|
|
jocblack wrote: testwin1Dlg.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
So are you linking with the correct library.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Under Additional Dependencies for your Linker, add "ws2_32.lib" I think it was. Anytime you use functions that aren't compiled from inside your solution/project you need to include the .h AND the .lib file. The only other way is to load the DLL at runtime, but I don't have experience with that.
Good luck
|
|
|
|
|
I was able to force the linker to use "ws2_32.lib" by adding the following line in my code.
#pragma comment(lib, "ws2_32.lib")
I found this in MSDN but it took a lot of search.
Thanks
|
|
|
|
|
hi i am having an bmp image which im loading into a button. the problem is the image differs from dialog .
what i have to do so that the image will be same resolution as dilaog.
Arise Awake Stop Not Till ur Goal is Reached.
|
|
|
|
|
deeps_cute wrote: the problem is the image differs from dialog
Huh?
Differs how? How are they related?
By default, button images are centered and clipped. Do you need to resize the button so the
image fits or resize the image so it fits the button?
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Do you want to change size of image ?
|
|
|
|
|
no actually the image should be transparent with the button
Arise Awake Stop Not Till ur Goal is Reached.
|
|
|
|
|
|
|
If you create an masked image list from the bitmap and add that to the button you should be able
to get transparency.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
I have a template function.
template <class T > void MyClass::DoSomething( int tag, T& value )
{
}
and it gets called like :
MyClass myClass;
double d = 0.0;
myClass.DoSomething <double >( 2, d );
When the function is in the C++ file it produces a link error (LNK2019).
If I place the function in the H file, all goes well.
any ideas ?
Thanks.
|
|
|
|
|
mmm,
an element type is a class? Because you are using the double "d" as the class in the second parameter of your template "T& value".
And the second thing is that the template expects a reference, and you are passing the "object" itself.
try with: double* d = 0.0;
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Nelek wrote: an element type is a class?
Nelek wrote: the template expects a reference, and you are passing the "object" itself.
Both seems perfectly right to me.
I am not sure if I got your first point right, but the second point is the proper way of calling a function with a reference-argument.
Failure is not an option - it's built right in.
|
|
|
|
|
an element type is a class? Because you are using the double "d" as the class in the second parameter of your template "T& value".
No. This is a template declaration and therefore the keyword class [or typename] stands for "some type".
And the second thing is that the template expects a reference, and you are passing the "object" itself.
try with: double* d = 0.0;
Again no. The terms you are using are messed up. First, double *d doesn't declare a reference but a pointer. Second, if it were, it would be wrong too; no need [even meaningless] to declare a reference to pass a function that takes its argument as a reference. You do provide a variable which is passed into the function by a reference value, and this is all.
--
=====
Arman
|
|
|
|
|
Maximilien wrote: When the function is in the C++ file it produces a link error (LNK2019).
If I place the function in the H file, all goes well.
This is the expected behaviour: At the time of instantiating a DoSomething -version with double as template-parameter, the compiler needs the full definition of DoSomething .
So it has to be in the .h -file.
The easiest part is to just live with it.
And possibly have DoSomething call type-specific worker-functions from some cpp.
Failure is not an option - it's built right in.
|
|
|
|
|
When the function is in the C++ file it produces a link error (LNK2019).
You mean source file?
Anyway, you would better to show the class declaration. I guess it is in a header file. Thus the linker cannot bind the definition with a [proper] declaration. The simpliest way to solve the problem is moving the function definition into the header file [make it inline if you would like to define it outside the class declaration].
--
=====
Arman
|
|
|
|