XOR NOT in C++ Like in VB6


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: XOR NOT in C++ Like in VB6

  1. #1
    Join Date
    May 2009
    Posts
    2

    XOR NOT in C++ Like in VB6

    Hi, I have a function in VB6 like this:

    Code:
    tot = CLng(tot Xor Not lngdata)
    I'd like to do the same in c++, but I can't get the same results...

    I've tried like this:

    Code:
    tot = (int)((-1) * (tot ^ lngdata) );
    But I don't get exactly the same results....
    Can anyone advise me why the above does not work as expected?

    Many thanks in advance!

  2. #2
    Join Date
    May 2007
    Posts
    843
    What is the datatype of tot and lngdata?

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    specify which datatypes you're using and break the complex expression down into individual operations, to avoid precedence problems.
    Danny Kalev

  4. #4
    Join Date
    May 2009
    Posts
    2

    Thumbs up

    Quote Originally Posted by Peter_APIIT View Post
    What is the datatype of tot and lngdata?
    They are all int type....

    I've solved like this:

    Code:
    tot = (tot ^ ~lngdata);

  5. #5
    Join Date
    Dec 2007
    Posts
    401
    > They are all int type....

    in general, prefer using an unsigned integral type for variables involving bit-wise operations. or better still, use a std::bitset<>.

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    Quote Originally Posted by vijayan View Post
    > They are all int type....

    in general, prefer using an unsigned integral type for variables involving bit-wise operations. or better still, use a std::bitset<>.
    The sign bit on signed integers sometimes is not set with logic operations, most notably this fails on unix systems while most window's compilers are more than happy to set the sign bit. Still, its not portable to use unsigned ones so at the very least, cast to unsigned first.

    Bitset is sometimes impractical due to being difficult (well, ugly at least) to get the integer value back out of it, and is more useful for storing a bunch of unrelated one-bit bools than straight up logic on integers, in my opinion of course. They just feel clunky to me.

    Finally, note that almost always, if an equals combo operator is available, its probably more efficient to use them.
    X = X^Y;
    should be
    X ^= Y;
    at the worst, they are the same, at best, the second might be faster, and the second is more concise too.


    Also, note the difference between logical expressions and binary ones. !x is quite different from ~x while x&y is quite different from x && y, yet due to the nice shorthand of C, its easy to mix them up.
    Last edited by jonnin; 05-09-2009 at 01:11 PM.

Similar Threads

  1. Replies: 0
    Last Post: 11-08-2008, 12:53 AM
  2. Speaking of strings...
    By Harlow in forum .NET
    Replies: 246
    Last Post: 10-26-2002, 12:30 AM
  3. Replies: 3
    Last Post: 07-21-2001, 02:32 PM
  4. Replies: 24
    Last Post: 02-09-2001, 12:18 PM
  5. Com Interop and VB6 Collections
    By Esmond Hart in forum .NET
    Replies: 2
    Last Post: 01-29-2001, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center