When FALSE is TRUE

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In a .Net application, given a C++ dll with methods called by a C# GUI

Assume (x&y) is FALS

the following code in the C++ dll, when called from the C# interface

bool theMethod(unsigned int x)

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y
return true
els
return false


1. Does NOT print the text "True
2. Does return TRU

While the following code

theMethod(unsigned int x

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y

printf("")
return true

els

printf("")
return false



1. Does NOT print the text "True
2. Does return FALS

Question
Why is this happening?
 
Correction to the code fragment

Bothe versions of "theMethod" return "bool

bool theMethod(unsigned int x

...



----- Honus wrote: ----

In a .Net application, given a C++ dll with methods called by a C# GUI

Assume (x&y) is FALS

the following code in the C++ dll, when called from the C# interface

bool theMethod(unsigned int x)

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y
return true
els
return false


1. Does NOT print the text "True
2. Does return TRU

While the following code

bool theMethod(unsigned int x

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y

printf("")
return true

els

printf("")
return false



1. Does NOT print the text "True
2. Does return FALS

Question
Why is this happening?
 
if (x&y)


Since I work in VB, I may not be best person to suggest this
but shouldn't there be two ampersands (&&)?
 
Josip Medved said:
Since I work in VB, I may not be best person to suggest this
but shouldn't there be two ampersands (&&)?

No - x&y does a bitwise AND operation.
 
Since I work in VB, I may not be best person to suggest this
No - x&y does a bitwise AND operation.


true.
but from source code it seems to me that he is trying to perform
logical AND operation since he is checking for true and false.
 
Josip Medved said:
true.
but from source code it seems to me that he is trying to perform
logical AND operation since he is checking for true and false.

In C/C++, any non-zero value is regarded as true. So basically,

if (x&y)

evaluates to true if both x and y are non-zero.
 
Hi Jon

How about when x = 1 and y = 2? The bitwise AND of these is zero, so surely
the statement should evaluate to false.

Also, pre-empting a reply by the OP, I took it that his issue is the
apparent inconsistency of the result, not that it evaluates to one true or
false for given values of x and y.

Charles
 
Charles Law said:
How about when x = 1 and y = 2? The bitwise AND of these is zero, so surely
the statement should evaluate to false.

Apologies - yes, indeed it should. I'm half asleep :)
Also, pre-empting a reply by the OP, I took it that his issue is the
apparent inconsistency of the result, not that it evaluates to one true or
false for given values of x and y.

Indeed.
 
Howdy

For some reason, after my initial posting of this query, I was unable to see it (or any responses). In the code example cited, the values of the unsigned ints will always be either 1 or 0. So the test works by returning true if either value is set to 1. There is no condition where a value will be equal to 2
However, as noted, the basic issue I have is: Why is the return value (at least as interpreted by the calling, C# method), dependent upon a bogus printf statement, rather than what the statement actually evaluates to
No one seems to have an answer to that question, and quite frankly, it makes me quite skeptical of the entire development environment (not that it takes much to arouse my skepticism where Microsoft is concerned)


----- Jon Skeet [C# MVP] wrote: ----

Charles Law said:
How about when x = 1 and y = 2? The bitwise AND of these is zero, so surel
the statement should evaluate to false

Apologies - yes, indeed it should. I'm half asleep :
Also, pre-empting a reply by the OP, I took it that his issue is th
apparent inconsistency of the result, not that it evaluates to one true o
false for given values of x and y

Indeed
 
Back
Top