the OR operator

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

    #1

    the OR operator

    hi, i'm sorry to bother you; but i was wondering what the function of the OR
    operator was in terms of numbers:

    num0 = num1 or num2

    i wrote me a little thing to try and see the effects and if the numbers are
    close, they add up; if they're real far apart, it gives the bigest only, and
    sometimes it gives the biggest +1 or +2.

    could someome please confirm the effects.
    thanks in advance
    Dan.


  • Lewis Gardner

    #2
    Re: the OR operator


    num0 = num1 or num0 = num2

    Dan. wrote:[color=blue]
    > hi, i'm sorry to bother you; but i was wondering what the function of the OR
    > operator was in terms of numbers:
    >
    > num0 = num1 or num2
    >
    > i wrote me a little thing to try and see the effects and if the numbers are
    > close, they add up; if they're real far apart, it gives the bigest only, and
    > sometimes it gives the biggest +1 or +2.
    >
    > could someome please confirm the effects.
    > thanks in advance
    > Dan.
    >
    >[/color]

    Comment

    • Auric__

      #3
      Re: the OR operator

      On Sat, 5 Jun 2004 16:26:55 +0200, Dan. wrote:
      [color=blue]
      >hi, i'm sorry to bother you; but i was wondering what the function of the OR
      >operator was in terms of numbers:
      >
      > num0 = num1 or num2
      >
      >i wrote me a little thing to try and see the effects and if the numbers are
      >close, they add up; if they're real far apart, it gives the bigest only, and
      >sometimes it gives the biggest +1 or +2.
      >
      >could someome please confirm the effects.
      >thanks in advance
      > Dan.[/color]

      It's a logical operator. For logic tests, it says "if either value is
      true, then the test is true".

      For numbers, it works at the bit level. If either bit is 1, the result
      is 1. The result is only 0 if both bits are 0.

      10010110 (decimal 150)
      OR 10110101 (decimal 181)
      -----------
      10110111 (decimal 183)

      To play around with the numbers, just open the windows calculator and
      switch to scientific mode. On the right side of the display are the
      logic buttons. (It's easier to see what's happening if you use binary
      numbers (select Bin at the top).)
      --
      auric underscore underscore at hotmail dot com
      *****
      Face down, arms out
      Nailed to the cross of doubt
      Blood runs like rain
      Drowning for this world in vain
      Crown of black thorns
      Human skin, ripped and torn
      Where is your savior now?
      Where is your savior now?
      -- Fear Factory, "Pisschrist "

      Comment

      • Dan.

        #4
        Re: the OR operator

        but what's the criteria for defining which statement executes?

        "Lewis Gardner" <lgardner@simpl ifiedtechnologi es.com> a écrit dans le
        message de news:40c1da1f$1 _1@news.iglou.c om...[color=blue]
        >
        > num0 = num1 or num0 = num2
        >
        > Dan. wrote:[color=green]
        > > hi, i'm sorry to bother you; but i was wondering what the function of[/color][/color]
        the OR[color=blue][color=green]
        > > operator was in terms of numbers:
        > >
        > > num0 = num1 or num2
        > >
        > > i wrote me a little thing to try and see the effects and if the numbers[/color][/color]
        are[color=blue][color=green]
        > > close, they add up; if they're real far apart, it gives the bigest only,[/color][/color]
        and[color=blue][color=green]
        > > sometimes it gives the biggest +1 or +2.
        > >
        > > could someome please confirm the effects.
        > > thanks in advance
        > > Dan.
        > >
        > >[/color][/color]


        Comment

        • Randy Birch

          #5
          Re: the OR operator

          The results can be different, so OR and + are not truly interchangeable .
          Consider:

          x=1
          y=3
          z1 = x Or y
          z2 = x + y

          ? z1, z2[color=blue]
          > 3 4[/color]

          OR (and AND) are logical operators which in conjunction with grouping
          parentheses, can be used to build sophisticated logical expressions. On
          numeric values OR performs what is called a "bitwise disjunction" on the two
          numeric values. This makes it handy for comparisons such as:

          w=5
          x=1
          y=3

          if (x or w) < (y or w) then...

          as:

          Debug.Print (x Or w) < (y Or w),
          Debug.Print (x + w) < (y + w)

          Debug.Print (x Or w), (y Or w)
          Debug.Print (x + w), (y + w)
          [color=blue]
          > True True[/color]
          5 7
          6 8





          --

          Randy Birch
          MVP Visual Basic

          Please respond only to the newsgroups so all can benefit.


          "Dan." <no@ddress.fr > wrote in message news:c9sl7j$elp $1@news.tiscali .fr...
          : hi, i'm sorry to bother you; but i was wondering what the function of the
          OR
          : operator was in terms of numbers:
          :
          : num0 = num1 or num2
          :
          : i wrote me a little thing to try and see the effects and if the numbers
          are
          : close, they add up; if they're real far apart, it gives the bigest only,
          and
          : sometimes it gives the biggest +1 or +2.
          :
          : could someome please confirm the effects.
          : thanks in advance
          : Dan.
          :
          :

          Comment

          • Randy Birch

            #6
            Re: the OR operator

            : but what's the criteria for defining which statement executes?

            What 'statement' do you mean? Lewis' example or yours?

            Your code "num0 = num1 or num2" says "num0 equals the bitwise addition of
            the values contained in num1 and num2". It is one expression that is fully
            evaluated.

            Lewis's code is - a bit more confusing - ... has:

            num0 = num1 or num0 = num2

            .... In VB this line is actually evaluated as representing the equation:

            num0 = num1 Or (num0 = num2)

            ... which therefore assigns num0 the result of a bitwise addition of num1
            with the result of the expression "num0 equals num2" (a true or false
            value).

            Thus, if:

            num0 = 2
            num1 = 1
            num2 = 3

            num0 = num1 Or (num0 = num2)
            Print num0[color=blue]
            > 1[/color]

            whereas if:

            num0 = 4
            num1 = 2
            num2 = 1

            num0 = num1 Or (num0 = num2)
            Print num0[color=blue]
            > 2[/color]

            Regardless, all parts of the line (expression) are evaluated .. there is no
            'short circuiting' in one-line And/Or statements in VB.
            --

            Randy Birch
            MVP Visual Basic

            Please respond only to the newsgroups so all can benefit.


            "Dan." <no@ddress.fr > wrote in message news:c9smrj$hsf $1@news.tiscali .fr...

            :
            : "Lewis Gardner" <lgardner@simpl ifiedtechnologi es.com> a icrit dans le
            : message de news:40c1da1f$1 _1@news.iglou.c om...
            : >
            : > num0 = num1 or num0 = num2
            : >
            : > Dan. wrote:
            : > > hi, i'm sorry to bother you; but i was wondering what the function of
            : the OR
            : > > operator was in terms of numbers:
            : > >
            : > > num0 = num1 or num2
            : > >
            : > > i wrote me a little thing to try and see the effects and if the
            numbers
            : are
            : > > close, they add up; if they're real far apart, it gives the bigest
            only,
            : and
            : > > sometimes it gives the biggest +1 or +2.
            : > >
            : > > could someome please confirm the effects.
            : > > thanks in advance
            : > > Dan.
            : > >
            : > >
            :
            :

            Comment

            • Randy Day

              #7
              Re: the OR operator

              "Dan." wrote:[color=blue]
              >
              > hi, i'm sorry to bother you; but i was wondering what the function of the OR
              > operator was in terms of numbers:
              >
              > num0 = num1 or num2
              >[/color]

              Think of it in terms of the actual bits used by
              the computer. Any bit position that has a 1 in
              either input number will have a 1 in the output
              number:

              num1 1011 (11) 0000 (0)
              num2 OR 0110 (6) OR 0110 (6)
              ---- ----
              num0 1111 (15) 0110 (6)

              AND works the opposite; any bit position that
              doesn't have both 1's will output 0:

              1011 (11) 1111 (15)
              AND 0000 (0) AND 0010 (2)
              ---- ----
              0000 (0) 0010 (2)

              --------------------------------------------

              'Bitmasking', as it's called, can be useful if
              you have a lot of yes/no type data to store
              really compactly.

              Let's say we have an 8-bit variable CarData;
              each bit can represent some feature that is
              or is not found on a particular car:

              stock/enhanced CD player------
              sport package--------------- |
              power steering------------ | |
              power door locks-------- | | |
              convertible----------- | | | |
              turbocharger-------- | | | | |
              leather seats----- | | | | | |
              ABS-------------+ | | | | | | |
              0 0 0 0 0 0 0 0

              Let's say we have a car with a stock CD player
              (its CarData bit 1 is 0), and we want to indicate
              that it now has the enhanced player (bit 1 = 1).
              BUT, we don't want any other car info to change!

              if CarData and 1 = 0 then 'test the value of bit 1
              CarData = CarData or 1 'CarData bit 1 is now 1
              end if

              This is how you set a single bit to zero without
              changing any other bits - AND it with a zero bit:

              10000101 (133)
              AND 11111110 (254)
              --------
              10000100 (132)

              if CarData and 1 = 1 then
              CarData= CarData and 254 'only the value of bit 1 changes
              end if



              Clear as mud? ;-)

              Comment

              • Dan.

                #8
                Re: the OR operator

                yep, i get it now. thanks for your help.

                "Randy Day" <ruthal@sasktel .nex> a écrit dans le message de
                news:40C1EB4B.F F7BAC0A@sasktel .nex...[color=blue]
                > "Dan." wrote:[color=green]
                > >
                > > hi, i'm sorry to bother you; but i was wondering what the function of[/color][/color]
                the OR[color=blue][color=green]
                > > operator was in terms of numbers:
                > >
                > > num0 = num1 or num2
                > >[/color]
                >
                > Think of it in terms of the actual bits used by
                > the computer. Any bit position that has a 1 in
                > either input number will have a 1 in the output
                > number:
                >
                > num1 1011 (11) 0000 (0)
                > num2 OR 0110 (6) OR 0110 (6)
                > ---- ----
                > num0 1111 (15) 0110 (6)
                >
                > AND works the opposite; any bit position that
                > doesn't have both 1's will output 0:
                >
                > 1011 (11) 1111 (15)
                > AND 0000 (0) AND 0010 (2)
                > ---- ----
                > 0000 (0) 0010 (2)
                >
                > --------------------------------------------
                >
                > 'Bitmasking', as it's called, can be useful if
                > you have a lot of yes/no type data to store
                > really compactly.
                >
                > Let's say we have an 8-bit variable CarData;
                > each bit can represent some feature that is
                > or is not found on a particular car:
                >
                > stock/enhanced CD player------
                > sport package--------------- |
                > power steering------------ | |
                > power door locks-------- | | |
                > convertible----------- | | | |
                > turbocharger-------- | | | | |
                > leather seats----- | | | | | |
                > ABS-------------+ | | | | | | |
                > 0 0 0 0 0 0 0 0
                >
                > Let's say we have a car with a stock CD player
                > (its CarData bit 1 is 0), and we want to indicate
                > that it now has the enhanced player (bit 1 = 1).
                > BUT, we don't want any other car info to change!
                >
                > if CarData and 1 = 0 then 'test the value of bit 1
                > CarData = CarData or 1 'CarData bit 1 is now 1
                > end if
                >
                > This is how you set a single bit to zero without
                > changing any other bits - AND it with a zero bit:
                >
                > 10000101 (133)
                > AND 11111110 (254)
                > --------
                > 10000100 (132)
                >
                > if CarData and 1 = 1 then
                > CarData= CarData and 254 'only the value of bit 1 changes
                > end if
                >
                >
                >
                > Clear as mud? ;-)[/color]


                Comment

                Working...