Announcement

Collapse
No announcement yet.

How to AND/OR an OR-ed value?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to AND/OR an OR-ed value?

    I'm quite sure I have seen it very recently in one of these forums, but I cannot find it any more. One of the causes is, maybe, that it is impossible to search for words like "AND" (the operator), because this is an operator in the forums' search engine as well.

    I need to know how to check the presence of a single value included in an "OR-ed" value. For example: when using GetDateFormat (Windows API), I need to do some editing in the returned date string. But this should absolutely not be done in case it is a short date. In other words: how should I "AND/OR" the dwFlags-setting passed through this function to be sure that a short date (%DATE_SHORTDATE) is not included?

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

    #2
    try:
    Code:
        myvalue AND %DATE_SHORTDATE = %DATE_SHORTDATE

    Comment


      #3
      I'm thinking you mean something like this?


      IF (lngBaseValue AND %SomeBitValue) = %SomeBitValue THEN DoSomething
      <b>George W. Bleck</b>
      <img src='http://www.blecktech.com/myemail.gif'>

      Comment


        #4
        In other words: how should I "AND/OR" the dwFlags-setting passed through this function to be sure that a short date (%DATE_SHORTDATE) is not included?
        To turn off a bit or ensure it's off....
        Code:
           NewBitmask =  OldBitmask AND (NOT %CONSTANTOFINTEREST)
        BIT RESET works, too, but then you can't use the symbolic constants like %DATE_SHORTDATE without looking up which bit number the value represents.

        I guess you could MACRO that to end up with a little more understandable in-line code...
        Code:
        MACRO TurnOff (mask, constant) =  Mask AND (NOT constant)
        ...
        
             dwFlags = Turnoff (dwFlags, %DATE_SHORTDATE)



        MCM
        Last edited by Michael Mattias; 25 Feb 2008, 12:04 PM.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


          #5
          Many thanks

          Stupid, stupid and once again: stupid me. Completely forgot this "trick". Could not find it in inside existing source code too, although I know that I've used it many times. Anyhow/anyway, all is working well now. Thank you very much for your contributions. This community is a great one!

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
          http://zijlema.basicguru.eu
          *** Opinions expressed here are not necessarily untrue ***

          Comment


            #6
            What about add and subtract?

            If 2 flags should be combined, the programmer writes:
            Code:
            %FLAG1 [B]OR[/B] %FLAG2
            But "under water" those 2 values are simply added. Is it safe to use adding and subtracting instead of OR'ing and AND'ing?

            Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
            http://zijlema.basicguru.eu
            *** Opinions expressed here are not necessarily untrue ***

            Comment


              #7
              Of course not, or and and are totally different to adding and subtracting ie 00000010 or 00000010 gives 00000010, added they give 00000100 and subtracted they give 00000000

              Comment


                #8
                Originally posted by Egbert Zijlema View Post
                If 2 flags should be combined, the programmer writes:
                Code:
                %FLAG1 [B]OR[/B] %FLAG2
                But "under water" those 2 values are simply added. Is it safe to use adding and subtracting instead of OR'ing and AND'ing?
                The short answer here is "no."

                Let's say you have 2 byte variables, x and y. If x is 3 (&b00000011) and y is 4 (&b00000100), ORing them and adding them together would have the same result, but if y instead contained the value 2 (&b00000010) and you tried to add them together, you would get 5 (&b00000101) whereas by ORing them together you would get 3 (&b00000011).

                If you can guarantee that no bits in the numbers you're adding together will be set in more than one value, it is safe to add rather than OR them together, but this just adds an unnecessary stipulation on things. The converse applies with ANDing/subtracting, except it's a bit more complicated.

                In both cases, it's both safer and simpler just to use the logical operators.
                Last edited by Eric Cochran; 26 Feb 2008, 09:10 AM. Reason: Didn't answer question fully
                Software: Win XP Pro x64 SP2, PB/Win 8.04, PB/CC 4.04
                Hardware: AMD Athlon 64 3200+ (Clock speed 2.00 GHz) on a Gigabyte K8N Pro nForce3-150 mobo, 1 GB PC3200/DDR400 RAM, GeForce 7300 GT 256 MB DDR2 video

                Comment


                  #9


                  http://www.intelifaqs.com/BitMaskView.zip

                  Comment


                    #10
                    >But "under water" those 2 values are simply added.

                    I do believe that will be news to the compiler publisher.

                    It will be true that....
                    Code:
                    (A OR B ) = (A + B)
                    .. but only when both A and B are integral powers of two AND not equal to each other..

                    No, it's not safe to add, unless you know 'A' and 'B' are nonequal integral powers of two.

                    However, even in this case ADDing instead of ORing will cost you ALL your 'good programming practice' points for the next six months. Maybe longer.

                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                      #11
                      Originally posted by Michael Mattias View Post
                      >But "under water" those 2 values are simply added.

                      I do believe that will be news to the compiler publisher.

                      It will be true that....
                      Code:
                      (A OR B ) = (A + B)
                      .. but only when both A and B are integral powers of two AND not equal to each other..

                      No, it's not safe to add, unless you know 'A' and 'B' are nonequal integral powers of two.

                      However, even in this case ADDing instead of ORing will cost you ALL your 'good programming practice' points for the next six months. Maybe longer.

                      MCM

                      Hmmmmmmmmm...

                      Not even then, I'm afraid--

                      2 OR 4 -->> 4

                      Comment


                        #12
                        Brad, your bitmask viewer is a keeper. Thanks.

                        Any chance of getting the source code?
                        ... Just found it at: http://www.powerbasic.com/support/fo...ML/003520.html
                        Last edited by Charles Dietz; 26 Feb 2008, 11:12 AM.

                        Comment


                          #13
                          Not even then, I'm afraid--

                          2 OR 4 -->> 4

                          Comment


                            #14
                            >2 OR 4 -->> 4


                            BUG REPORT.
                            PB/Win 8.03 compiler gets something different.
                            Code:
                            #COMPILE EXE
                            #DIM ALL
                            
                            FUNCTION PBMAIN () AS LONG
                            
                                LOCAL A AS LONG, B AS LONG, C AS LONG
                                
                                A =2&
                                B =4&
                                C = (A OR B)
                                MSGBOX USING$ (" # OR # = #", A, B, C)
                            
                            END FUNCTION
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                              #15
                              Integral powers of 2?

                              This is (in pseudo code) the listing of values involved (source: Win32Api.inc, the values are real, only the names are fake):
                              Code:
                              %FLAG1 = &H00000001     
                              %FLAG2 = &H00000002     
                              %FLAG3 = &H00000004     
                              %FLAG4 = &H00000008     
                              %FLAG5 = &H00000010
                              %FLAG6 = &H00000020
                              When I "OR" some of these values the result appears to be the sum, for instance: %FLAG1 OR %FLAG5 = 17, which is the same as %FLAG1 + %FLAG5. What I also need is a sequential %FLAG7. Should its value be 48 (&H00000030) or 64 (&H00000040)?
                              Now the real problem: when %FLAG7 is passed, my function should initially execute as if %FLAG2 had been passed (and finally do a minor edit to the result in order to match %FLAG7). Let's assume the combined values are passed as dwCombi (DWORD). This is what my function initially should do:
                              • check if %FLAG7 is one of the values in dwCombi
                              • if so, extract %FLAG7 from dwCombi and add %FLAG2 (note: flags 7 and 2 exclude each other and can never be OR'ed)


                              This is the code I've written and it appears to work. Is it correct and safe?
                              Code:
                              IF (dwCombi AND %FLAG7) THEN
                                 dwCombi = dwCombi AND (NOT %FLAG7)
                                 dwCombi = dwCombi OR %FLAG2
                                 lRebuildFlag = 1    ' remember %FLAG7 was initially passed
                              END IF
                              Last edited by Egbert Zijlema; 26 Feb 2008, 11:41 AM. Reason: wrong var name

                              Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                              http://zijlema.basicguru.eu
                              *** Opinions expressed here are not necessarily untrue ***

                              Comment


                                #16
                                >for instance: %FLAG1 OR %FLAG5 = 17, which is the same as %FLAG1 + %FLAG5.

                                Of course it is. FLAG1 and FLAG5 are nonequal integral powers of two.

                                >Now the real problem: when %FLAG7 is passed, my function should initially execute as >if %FLAG2 had been passed

                                Hell of a design. I would have just passed FLAG2 when I wanted FLAG2.

                                If you will take some free advice: Just forget about adding and use the booleans

                                Set a bit flag : newmask = (oldmask OR desiredmask)
                                Turn off : newMask = (OldMask AND (NOT DesiredMask))
                                Is set? : ISTRUE (newMask and DesiredMask)

                                Yes, I always use all those parens. With "short-circuit" valuation in effect I'd rather the compiler know EXACTLY what I mean.

                                FWIW..
                                This is the code I've written and it appears to work
                                Code:
                                IF (dwCombi AND %FLAG7) THEN
                                   dwCombi = dwCombi AND (NOT %FLAG7)
                                   dwCombi = dwCombi OR %FLAG2
                                   lRebuildFlag = 1    ' remember %FLAG7 was initially passed
                                END IF
                                It's certainly 'safe'.

                                It's correct if what you want is "If the mask comes in with FLAG7 on, turn it off, set FLAG2 in the mask and set the rebuild flag."

                                I'd add ELSE iRebuildFlag = %FALSE myself, since it might be set elsewhere in the code (not shown). It also makes it very clear what you are doing with that rebuild flag if you don't look at this code sometime in the next six months.

                                (I'd also add the parens).




                                MCM
                                Last edited by Michael Mattias; 26 Feb 2008, 12:16 PM.
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                  #17
                                  OK, OK--

                                  See what happens when you start working at 3am? You can't count any more! {smile}

                                  Bob

                                  Comment


                                    #18
                                    >See what happens when you start working at 3am?

                                    Hmm.... now I can't hardly wait for the Enhanced Boolean Operators in PB/Win 9.0!!!


                                    MCM
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                      #19
                                      Originally posted by Michael Mattias View Post
                                      Hmm.... now I can't hardly wait for the Enhanced Boolean Operators in PB/Win 9.0!!!
                                      Exclusive AND - A logical function of the highest quality performed on only a limted amount of data.
                                      Last edited by Joseph Cote; 26 Feb 2008, 12:47 PM.
                                      The boy just ain't right.

                                      Comment


                                        #20
                                        Thanks

                                        Hello folks,

                                        Thanks to everyone who made a useful contribution to this discussion; especially MCM who gave a crystalclear answer that definitely solves my long-existing problems in this field. Thanks Michael!

                                        Only one minor question is unanswered so far. Which value must I give to the new constant (%FLAG7): 48 OR 64? The latter is an integral power of 2 (2^6), so - given the discussion here - I tend to prefer 64.

                                        Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                                        http://zijlema.basicguru.eu
                                        *** Opinions expressed here are not necessarily untrue ***

                                        Comment

                                        Working...
                                        X
                                        😀
                                        🥰
                                        🤢
                                        😎
                                        😡
                                        👍
                                        👎