Board index » cppbuilder » opposite of |=

opposite of |=


2007-11-16 05:06:51 AM
cppbuilder63
Some Windows API functions take a DWORD (e.g. dwFlags) as input and you can
for instance build that variable by adding parts during a process that
determines the best combination:
E.g. dwFlags |= DO_OR_DONT_DO_STUFF ;
Now, what if I want to remove a part again ?
What does the exact opposite of |= (regardless if that part was already set
or not) and leaves all the rest intact ?
 
 

Re:opposite of |=

"Peter" < XXXX@XXXXX.COM >wrote in
message news: XXXX@XXXXX.COM ...
Quote
Now, what if I want to remove a part again ?
Use the '&' and '~' operators, ie:
dwFlags &= ~DO_OR_DONT_DO_STUFF ;
Gambit
 

Re:opposite of |=

Thanks !
Quote
>Now, what if I want to remove a part again ?

Use the '&' and '~' operators, ie:

dwFlags &= ~DO_OR_DONT_DO_STUFF ;


Gambit
 

{smallsort}

Re:opposite of |=

Well, let assume that dwFlags == DO_OR_DONT_DO_STUFF . So after the
operation
dwFlags != DO_OR_DONT_DO_STUFF
we will get that
dwFlags == DO_OR_DONT_DO_STUFF
will not?
However after the proposed *inverse* operation
dwFlags &= ~DO_OR_DONT_DO_STUFF
we will get that dwFlags will not be equal to its original value
DO_OR_DONT_DO_STUFF ! :)
Vladimir Grigoriev
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Peter" < XXXX@XXXXX.COM >wrote in
message news: XXXX@XXXXX.COM ...

>Now, what if I want to remove a part again ?

Use the '&' and '~' operators, ie:

dwFlags &= ~DO_OR_DONT_DO_STUFF ;


Gambit

 

Re:opposite of |=

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
Quote
Well, let assume that dwFlags == DO_OR_DONT_DO_STUFF . So after the
operation
dwFlags != DO_OR_DONT_DO_STUFF
we will get that
dwFlags == DO_OR_DONT_DO_STUFF
will not?

However after the proposed *inverse* operation

dwFlags &= ~DO_OR_DONT_DO_STUFF

we will get that dwFlags will not be equal to its original value
DO_OR_DONT_DO_STUFF ! :)

Vladimir Grigoriev
So the answer to the question is not correct. I think the question should be
asked another way. Instead of
"What does the exact opposite of |= (regardless if that part was already set
or not) and leaves all the rest intact ?"
How can I clear the bits which were set with the operation |=?
Vladimir Grigoriev
 

Re:opposite of |=

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
Quote
Well, let assume that dwFlags == DO_OR_DONT_DO_STUFF .
So after the operation
dwFlags != DO_OR_DONT_DO_STUFF
"!=" and "|=" are not the same thing. One is a comparison, the other is an
assignment.
Quote
we will get that
dwFlags == DO_OR_DONT_DO_STUFF
will not?
I'm not sure what you are trying to ask exactly.
Quote
However after the proposed *inverse* operation

dwFlags &= ~DO_OR_DONT_DO_STUFF

we will get that dwFlags will not be equal to its original value
DO_OR_DONT_DO_STUFF ! :)
Of course not. The original value had DO_OR_DONT_DO_STUFF enabled. After
"&=" and "~", DO_OR_DONT_DO_STUFF is removed from the value. That is the
whole point of using them. And that is what Peter asked for.
Gambit
 

Re:opposite of |=

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
Quote
So the answer to the question is not correct.
Yes, it is. Peter asked how to remove DO_OR_DONT_DO_STUFF from dwFlags
without removing anything else. The answer I gave him is the correct
solution to that question.
Quote
I think the question should be asked another way.
It doesn't matter how you phrase it. The end effect is still the same, and
so is the code for it.
Quote
Instead of "What does the exact opposite of |= (regardless if that
part was already set or not) and leaves all the rest intact ?"
How can I clear the bits which were set with the operation |=?
The answer I gave earlier is the same to that question as well. Using "&="
and "~" will turn off the bits that "|=" turned on.
Gambit
 

Re:opposite of |=

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote
"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Well, let assume that dwFlags == DO_OR_DONT_DO_STUFF .
>So after the operation
>dwFlags != DO_OR_DONT_DO_STUFF

"!=" and "|=" are not the same thing. One is a comparison, the other is an
assignment.

>we will get that
>dwFlags == DO_OR_DONT_DO_STUFF
>will not?

I'm not sure what you are trying to ask exactly.
I think he's trying to show the difference between "undoing" the |=
operator, and unsetting the corresponding bits.
The distinction is only evident when the bits are ALREADY set, prior
to ORing some more in with them. If you undo the |= operator, it
would only change bits that actually changed on the way in, but
unsetting the corresponding bits may leave the original value in a
different state.
For example, let's use a 3-bit binary number, and assume that we can
write binary literals with a 'b' suffix:
var = 110b;
mask = 111b;
var |= mask; // var now 111b (only changes 1 bit)
NOW: "Undo" will revert var to 110b, but unsetting the bits that the
mask OR'd in will revert var to 000b. His main point is this:
"ORing in a set of bits, then ANDing them out does NOT necessarily
leave the value in the original state."
I had to read his post several times to get what he was trying to
point out, but this is what i think it was.
--
Chris (TeamB);
 

Re:opposite of |=

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote
The answer I gave earlier is the same to that question as well. Using "&="
and "~" will turn off the bits that "|=" turned on.
That's *true*, but it's not complete, because &= may turn off some
bits that |= did not turn on (because they were already on.)
--
Chris (TeamB);
 

Re:opposite of |=

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
That's *true*, but it's not complete, because &= may turn off some
bits that |= did not turn on (because they were already on.)
Perhaps, but that is not what Peter originally asked for.
If the code really needs to be sensitive to what "|=" actually turned on,
then it has to keep track of the changed bits manually, ie:
DWORD dwOriginal = dwFlags;
dwFlags |= DO_OR_DONT_DO_STUFF;
DWORD dwChangedBits = dwOriginal ^ dwFlags;
//...
if( dwChangedBits != 0 )
dwFlags &= ~dwChangedBits;
Gambit
 

Re:opposite of |=

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
"ORing in a set of bits, then ANDing them out does NOT necessarily
leave the value in the original state."

I had to read his post several times to get what he was trying to
point out, but this is what i think it was.

--
Chris (TeamB);
Yes, I tried to say that. :)
Vladimir Grigoriev