Announcement

Collapse
No announcement yet.

How to AND/OR an OR-ed value?

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

  • Michael Mattias
    replied
    No Michael, you missed the point that the fourth basic for computing is the concept of time
    I do applications, not time.

    Regardless, XOR is IMNSHO one of the four basic boolean operators (along with AND, OR, and NOT) with which everyone who wants to call himself a programmer should be familiar.

    (You can leave the IMP club in the bag).

    MCM

    Leave a comment:


  • Cliff Nichols
    replied
    John, you missed a point,
    When thinking AND OR etc, you have to think "ElectroMechanical" or coding-wise you will get TOTALLY lost over a simple programming error.

    but just could not see the point in elegant solutions that only confused the understanding of basic boolean algebra
    Not sure how you meant, but to me seeing Boolean makes perfect sense vs higher levels of solutions in some cases

    (Although I still have to break down to "Bit-by-Bit" to keep my math straight when it comes to binary values)

    But at least I understand it rather than blindly saying %MyValue1 AND %MahVal2

    and expect to come up with Decimal val 4 and the reply is 5

    Leave a comment:


  • John Petty
    replied
    No Michael, you missed the point that the fourth basic for computing is the concept of time, a flip flop gained its name in that in its first incarnation it reversed its state on each clock cycle (the not of the output being fed back to the input) thus creating a binary divide be 2 function or combined a binary counter. Its primarily ability is that it can remember a state and so is the basis of what in programming we call today a register

    Leave a comment:


  • Michael Mattias
    replied
    The nearest thing to a fourth basic is a flip flop which ..can still be built with the three basic functions.
    To toggle (flip-flop) a bit you can use XOR (which might qualify as your 'fourth basic')

    Bit_Toggled_mask = OriginalMask XOR %FLAGn

    (I think)

    MCM

    Leave a comment:


  • John Petty
    replied
    Goodness me can't believe so many posts about such a basic. Binary computers use Boolean Algebra which has only 3 basic rules, AND, OR, NOT all other functions including addition, subtraction, exclusive or etc are derived from them and can be built using a combination of these three gates in electronics. The nearest thing to a fourth basic is a flip flop which intoduces the concept of a clock and can still be built with the three basic functions.
    This doesn't even qualify to be called computing 101
    Edit Afterfought, reply may seem a bit sarcastic but just could not see the point in elegant solutions that only confused the understanding of basic boolean algebra
    Last edited by John Petty; 27 Feb 2008, 10:15 AM.

    Leave a comment:


  • Cliff Nichols
    replied
    Just because I like seeing my leading zero's (so easy to forget when you are thinking about a different subject)

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32Api.INC"
    
    FUNCTION PBMAIN () AS LONG
    
         %FLAG0  = &b00000001
         %FLAG1  = &b00000010
         %FLAG2  = &b00000100
         %FLAG3  = &b00001000
         %FLAG4  = &b00010000
    MSGBOX BIN$(%FLAG0 AND %FLAG1, 8)
    MSGBOX BIN$(%FLAG0 OR %FLAG1, 8)
    MSGBOX BIN$(%FLAG0 AND %FLAG1 AND %FLAG2, 8)
    MSGBOX BIN$(%FLAG0 OR %FLAG1 OR %FLAG2, 8)
    MSGBOX BIN$(%FLAG0 AND %FLAG1 AND %FLAG2 AND %FLAG3, 8)
    MSGBOX BIN$(%FLAG0 OR %FLAG1 OR %FLAG2 OR %FLAG3, 8)
    MSGBOX BIN$(%FLAG0 AND %FLAG1 AND %FLAG2 AND %FLAG3 AND %FLAG4, 8)
    MSGBOX BIN$(%FLAG0 OR %FLAG1 OR %FLAG2 OR %FLAG3 OR %FLAG4, 8)
    
    END FUNCTION

    Leave a comment:


  • Egbert Zijlema
    replied
    I see the pattern, yes.

    Yes Michael, I see the pattern. Everything is clear now. Thanks again.

    Leave a comment:


  • Michael Mattias
    replied
    >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.

    ????

    This is a trick question, right?

    Ok, I'll bite anyway... you want %FLAG7 = EXP2(n) where 'n' is the (0-based) bit number you want "on" when %FLAG7 is to be "true."

    (Hint: if %flag7=48 more than one bit will have to be set which will screw up EVERYTHING!).

    BTW.. just forget about that 'integral power of two' crap I posted!. Use the boolean operators only!

    More hints
    Code:
    %FLAG0  = &b1
    %FLAG1  = &b10
    %FLAG2  = &b100 
    %FLAG3  = &b1000
    %FLAG4  = &b10000
    .....
    (Are you sensing a pattern yet?)

    MCM
    Last edited by Michael Mattias; 26 Feb 2008, 04:02 PM.

    Leave a comment:


  • Egbert Zijlema
    replied
    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.

    Leave a comment:


  • Joseph Cote
    replied
    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.

    Leave a comment:


  • Michael Mattias
    replied
    >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

    Leave a comment:


  • Bob Zale
    replied
    OK, OK--

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

    Bob

    Leave a comment:


  • Michael Mattias
    replied
    >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.

    Leave a comment:


  • Egbert Zijlema
    replied
    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

    Leave a comment:


  • Michael Mattias
    replied
    >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

    Leave a comment:


  • Paul Dixon
    replied
    Not even then, I'm afraid--

    2 OR 4 -->> 4

    Leave a comment:


  • Charles Dietz
    replied
    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.

    Leave a comment:


  • Bob Zale
    replied
    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

    Leave a comment:


  • Michael Mattias
    replied
    >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

    Leave a comment:


  • Brad D Byrne
    replied


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

    Leave a comment:

Working...
X