Re: Bit-related question

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 07/07/04


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++


Relevant Pages

  • Re: [C] Program has a math bug
    ... Jack Klein wrote: ... > First it performs the subtraction of one double value from the other. ... > takes one parameter of type int, it must convert the result of the ... Edd, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: mixing types promotion etc..
    ... > My question wat type does the result have when a double value is mixed ... In the snippet below I substract a double with an int ... since the other operand of the subtraction is a double. ... Perhaps that contributes to the confusion... ...
    (comp.lang.c)
  • Re: division by 7 efficiently
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... Therefore, our fraction is ... int divide_by_7 ...
    (comp.programming)
  • Re: whats the scope of i ?
    ... > int main ... /Ze enable extensions ... Copyright Microsoft Corporation 1984-2002. ... However in the OP compiler it does, as even his simpler code with two ...
    (comp.lang.cpp)
  • Re: sizeof on arrays and pointers
    ... >compilers I am using: ... >int main ... Here is a copy of my Whidbey beta compile session and output for this ... Copyright Microsoft Corporation. ...
    (microsoft.public.vc.language)