XOR NOT in C++ Like in VB6
Hi, I have a function in VB6 like this:
I'd like to do the same in c++, but I can't get the same results...
tot = CLng(tot Xor Not lngdata)
I've tried like this:
But I don't get exactly the same results....
tot = (int)((-1) * (tot ^ lngdata) );
Can anyone advise me why the above does not work as expected?
Many thanks in advance!
What is the datatype of tot and lngdata?
specify which datatypes you're using and break the complex expression down into individual operations, to avoid precedence problems.
They are all int type....
Originally Posted by Peter_APIIT
I've solved like this:
tot = (tot ^ ~lngdata);
> 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.
Originally Posted by vijayan
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;
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 02:11 PM.
By JSpyder in forum VB Classic
Last Post: 11-08-2008, 01:53 AM
Last Post: 10-26-2002, 01:30 AM
By Richard Curzon in forum .NET
Last Post: 07-21-2001, 03:32 PM
By Mark Burns in forum .NET
Last Post: 02-09-2001, 01:18 PM
By Esmond Hart in forum .NET
Last Post: 01-29-2001, 07:44 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center