-
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!
-
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.
Danny Kalev
-
 Originally Posted by Peter_APIIT
What is the datatype of tot and lngdata?
They are all int type....
I've solved like this:
Code:
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<>.
-
 Originally Posted by vijayan
> 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
-
By JSpyder in forum VB Classic
Replies: 0
Last Post: 11-08-2008, 12:53 AM
-
Replies: 246
Last Post: 10-26-2002, 12:30 AM
-
By Richard Curzon in forum .NET
Replies: 3
Last Post: 07-21-2001, 02:32 PM
-
By Mark Burns in forum .NET
Replies: 24
Last Post: 02-09-2001, 12:18 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks