When FALSE is TRUE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Honus

    #1

    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(unsig ned 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(unsig ned 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?
  • Honus

    #2
    RE: When FALSE is TRUE

    Correction to the code fragment

    Bothe versions of "theMethod" return "bool

    bool theMethod(unsig ned 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(unsig ned 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(unsig ned 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?

    Comment

    • Honus

      #3
      RE: When FALSE is TRUE

      Correction to the code fragment

      Bothe versions of "theMethod" return "bool

      bool theMethod(unsig ned 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(unsig ned 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(unsig ned 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?

      Comment

      • Josip Medved

        #4
        Re: When FALSE is TRUE

        > 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
        A personal website dedicated to technology, software development, and practical tools.



        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: When FALSE is TRUE

          Josip Medved <jmedved@jmedve d.com> wrote:[color=blue][color=green]
          > > if (x&y)[/color]
          >
          > Since I work in VB, I may not be best person to suggest this
          > but shouldn't there be two ampersands (&&)?[/color]

          No - x&y does a bitwise AND operation.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Josip Medved

            #6
            Re: When FALSE is TRUE

            > > Since I work in VB, I may not be best person to suggest this[color=blue][color=green]
            > > but shouldn't there be two ampersands (&&)?[/color][/color]
            [color=blue]
            > No - x&y does a bitwise AND operation.[/color]


            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
            A personal website dedicated to technology, software development, and practical tools.



            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: When FALSE is TRUE

              Josip Medved <jmedved@jmedve d.com> wrote:[color=blue][color=green][color=darkred]
              > > > Since I work in VB, I may not be best person to suggest this
              > > > but shouldn't there be two ampersands (&&)?[/color][/color]
              >[color=green]
              > > No - x&y does a bitwise AND operation.[/color]
              >
              >
              > 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.[/color]

              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 - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Charles Law

                #8
                Re: When FALSE is TRUE

                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]" <skeet@pobox.co m> wrote in message
                news:MPG.1b016f 0491251b3998a81 c@msnews.micros oft.com...[color=blue]
                > Josip Medved <jmedved@jmedve d.com> wrote:[color=green][color=darkred]
                > > > > Since I work in VB, I may not be best person to suggest this
                > > > > but shouldn't there be two ampersands (&&)?[/color]
                > >[color=darkred]
                > > > No - x&y does a bitwise AND operation.[/color]
                > >
                > >
                > > 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.[/color]
                >
                > 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 - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: When FALSE is TRUE

                  Charles Law <blank@nowhere. com> wrote:[color=blue]
                  > How about when x = 1 and y = 2? The bitwise AND of these is zero, so surely
                  > the statement should evaluate to false.[/color]

                  Apologies - yes, indeed it should. I'm half asleep :)
                  [color=blue]
                  > 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.[/color]

                  Indeed.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Honus

                    #10
                    Re: When FALSE is TRUE

                    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 <blank@nowhere. com> wrote[color=blue]
                    > How about when x = 1 and y = 2? The bitwise AND of these is zero, so surel
                    > the statement should evaluate to false[/color]

                    Apologies - yes, indeed it should. I'm half asleep :
                    [color=blue]
                    > 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[/color]

                    Indeed

                    --
                    Jon Skeet - <skeet@pobox.co m
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me to

                    Comment

                    Working...