 |
|
 |
Nice article and useful.
Here's one more that inverts the value of a particular bit (maybe it is there already be I 've missed it):
template <class T, class U, class V>
inline T invertBit(T value, U mask)
{
return (value & mask) ? setBits(value, mask) : clearBits(value, mask);
}
Jonathan de Halleux.
|
|
|
|
 |
|
 |
Eddie, What is the reason for using templates. I cannot figure out why would anybody want to use any thing other than integers with bitwise operations.
|
|
|
|
 |
|
 |
Yes, but there are integers of differen size: char, short, long, and also the unsigned ones.
I vote pro drink
|
|
|
|
 |
|
 |
There are a bunch of different kinds of integers: signed and unsigned chars, ints, longs, __int64s. Not one fits all possible needs.
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
Hi Eddie,
Are you sure it's a good idea to use ASSERT in the uppercase form? I'm not sure but it seems to me that ASSERT is the MFC specific form.
BTW, the set is very good. I've created some similar functions myself but your set is very comprehensive. Thanks! I'm going to switch to your version right away.
MK
|
|
|
|
 |
|
 |
You're absolutely right. The uppercased version (ASSERT) is the MFC version, the lowercased version (assert) is the C runtime version which is more portable. I've never noticed before because this code is used in a MFC projects. I'll fix the code anyway. Thank you for the suggestion.
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
I'd suggest to use _ASSERTE() - CRT version of ASSERT - if _MSC_VER is defined and assert() otherwise.
MK
|
|
|
|
 |
|
 |
I think _ASSERTE() is Microsoft-specific. assert() is the portable version. (Important if you care about portability! )
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
That's right but call to assert() terminates the process but with call to _ASSERTE you can choose to ignore it and go ahead.
Speaking strictly for myself: I'll sacrifice the portability for it.
MK
|
|
|
|
 |
|
 |
Mike Klimentiev wrote:
Speaking strictly for myself: I'll sacrifice the portability for it.
That's why the source code is provided: so whatever you want with it. I'm using the MFC version (ASSERT()) and it's ok for me.
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
I wrote these years ago that I use all the time. Not as complete,
but covers a lot of cases. Good if you want to use stright C or no templates.
// Determine if nBitNum (0-15) is set in wDataWord
#define IsBitSet(wDataWord,nBitNum) (((wDataWord) & 0x00000001 << (nBitNum)) ? TRUE:FALSE)
// Set nBitNum (0-15) in wDataWord
#define SetBit(wDataWord,nBitNum) ((wDataWord) |= 0x00000001 << (nBitNum))
// Clear nBitNum (0-15) in wDataWord
#define ClearBit(wDataWord,nBitNum) ((wDataWord) &= ~(WORD)(0x00000001 << (nBitNum)))
|
|
|
|
 |
|
 |
I also used preprocessor macro versions long time ago, but macros are too error prone. Anyway, they'll do the job! BTW, why wouldn't you want to use a templatized version (in C++ code)? I'm just curious about your reasons.
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
There is no reason not to use the template versions. I support a lot of
old C code, so I like to use the same macro for both C and C++. I am slowly dumping the macros.
I will start using your templates now...
Daniel
|
|
|
|
 |
|
 |
There is no reason not to use the template versions. I support a lot of old C code, so I like to use the same macro for both C and C++. I am slowly dumping the macros.
Yeah, if you have to support both C and C++ you have to stick with the macros.
I will start using your templates now...
Cool! I hope you find them useful. And... I'm open to any suggestions and yes... any bug reports too!
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
Very useful, thank you Eddie.
|
|
|
|
 |
|
 |
if(status == STAT_ONE || status == STAT_TWO)) DoSomething();
|
|
|
|
 |
|
 |
okigan wrote:
if(status == STAT_ONE || status == STAT_TWO))
DoSomething();
This means something completely different than
if(status & (STAT_ONE | STAT_TWO))
DoSomething();
Your example does a logical OR operation that means if status' value is either equal to STAT_ONE or equal to STAT_TWO execute DoSomething().
My example does a bitwise operation that means if either STAT_ONE or STAT_TWO bits are set then execute DoSomething(). The meaning of both operations is completely different.
The example in the article doesn't state the values of STAT_ONE and STAT_TWO to make the example obvious. I'll fix it!
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
Wrong, and that shows that bitwise operations are indeed not so intuitive, except perhaps for old assembly guys
If status == 0x1111, your test will fail, while the original one will succeed.
These tools are interesting, because they summarize the most frequent uses we make with these operations.
Well done.
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
|
|
|
|
 |
|
 |
In which example do you say it fails?
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |
|
 |
Sorry, I wasn't too clear.
By "your example", I meant okigan's one, since I replied to his message.
By "the original one", I meant your, Eddie's one
Regards.
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
|
|
|
|
 |
|
 |
I think this messagins thingie is not so intuitive
|
|
|
|
 |
|
 |
Oopps! My mistake!
Foot-and-Mouth disease is believed to be the first virus unable to spread through Microsoft Outlook
|
|
|
|
 |