Re: Bit-related question
From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 07/07/04
- Next message: Victor Bazarov: "Re: clock()"
- Previous message: Alex: "Re: clock()"
- In reply to: David Crow [MCSD]: "Bit-related question"
- Next in thread: Stephen Bye: "Re: Bit-related question"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 07 Jul 2004 13:52:03 -0500
David Crow [MCSD] wrote:
>My question mentions MFC, but it is actually just a matter of bit
>arithmetic.
>
>If you are working on an MFC application having a frame, seeing the
>following statement is not uncommon:
>
> cs.style &= ~FWS_ADDTITLE; // remove extra stuff from title
>
>My question is, why is the value simply not subtracted like:
>
> cs.style -= FWS_ADDTOTITLE;
>
>The resulting style is the same for both. Is there a style (i.e., value)
>that could exist such that subtracting would produce a different result than
>ANDing the one's complement? I've tried with several starting values but
>both ways produce the same value.
Subtraction is addition of a number that has been negated. For two's
complement signed ints, negation consists of adding one to the one's
complement. So subtraction is obviously different than simply doing a
bitwise AND in this case. It's also different for unsigned numbers. Here's a
little program you can play with:
#include <iostream>
using namespace std;
template<class T>
void test(T x, T y)
{
cout << typeid(T).name() << ":\n";
cout
<< x
<< " & ~"
<< y
<< "\t= "
<< (x & ~y)
<< '\n';
cout
<< x
<< " - "
<< y
<< "\t= "
<< (x - y)
<< '\n';
}
int main()
{
test(1, 2);
cout << "*****\n";
test(2, 1);
}
X>cl -W4 -GX a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
a.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.
/out:a.exe
a.obj
X>a
int:
1 & ~2 = 1
1 - 2 = -1
*****
int:
2 & ~1 = 2
2 - 1 = 1
-- Doug Harrison Microsoft MVP - Visual C++
- Next message: Victor Bazarov: "Re: clock()"
- Previous message: Alex: "Re: clock()"
- In reply to: David Crow [MCSD]: "Bit-related question"
- Next in thread: Stephen Bye: "Re: Bit-related question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|