Hi, I have a function in VB6 like this:

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:

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!

Recommended Answers

All 6 Replies

What's a problem?

tot ^= !lngdata;

Well read literally, you have tot = CLng( tot ^ !lngdata) But is XOR-NOT is some new operator which is the logical inverse of XOR (like NAND is to AND), then perhaps one of these

tot = CLng( !tot ^ !lngdata)
tot = CLng( !(tot ^ lngdata) )

Post a truth-table if you're still stuck.

The problem is that with

tot ^= !lngdata;

if lngdata is 0 in VB I get -1 and in c++ I get 1

if lngdata is 39942 in VB I get -39943 and in c++ I get 0...

Solved!

I've forgotten that in c++ the not operator is implemented with "~"

so the right code is:

tot = (tot ^ ~lngdata);

Many thanks anyway!

I've forgotten that in c++ the not operator is implemented with "~"

Just a little bit extra info: In C++ you've actually two NOT-operators (this does also apply to the AND and OR operators): you've the bitwise NOT-operator ~ and you also have the logical NOT-operator ! :) ...

You use the logical NOT-operator if you expect an expression to be true or false , the bitwise NOT is just wrapping all the bits of it's operand (1 becomes 0 and 0 becomes 1)
The NOT operators in C++ are both unary operators, which means that they both work with one operand.

commented: nice clarification in this discussion! +4

Of course, operator ~. What's a shame :( Sorry.

commented: Never mind, that's because you're used to solve the most sophisticated problems :P +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.