473,511 Members | 15,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When FALSE is TRUE

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?
Jul 21 '05 #1
9 1362
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?
Jul 21 '05 #2
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?
Jul 21 '05 #3
> if (x&y)
Since I work in VB, I may not be best person to suggest this
but shouldn't there be two ampersands (&&)?

--

Pozdrav,
Josip Medved, MCSD
http://www.jmedved.com
Jul 21 '05 #4
Josip Medved <jm*****@jmedved.com> wrote:
if (x&y)


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
> > 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.

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.

--

Pozdrav,
Josip Medved, MCSD
http://www.jmedved.com
Jul 21 '05 #6
Josip Medved <jm*****@jmedved.com> wrote:
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.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Josip Medved <jm*****@jmedved.com> wrote:
> 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.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #8
Charles Law <bl***@nowhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
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 <bl***@nowhere.com> wrote
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

--
Jon Skeet - <sk***@pobox.com
http://www.pobox.com/~skee
If replying to the group, please do not mail me to

Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1911
by: Privacy Trap | last post by:
Hi I have a slight problem in trying to set up 2 forms linked to tables. What I want to do is to inform a control on one form(Apps) as to the value of a key of a 'new' record entered via a...
10
3456
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
0
7233
by: Alex | last post by:
my app was working fine in VB.NET 2003 (and framework 1.1). Now with VB.NET 2005 (framework 2.0) the uploading to an http server (ie. www.sharebigfile.com) stops with the error "The request was...
38
10118
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
2
3427
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3...
1
2045
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
2
2341
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
1
2048
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
8
1711
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.