Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Weird Stuff (Basic)

Options
  • 16-01-2003 5:24pm
    #1
    Registered Users Posts: 6,240 ✭✭✭


    I use PowerBasic in work, varient of basic

    anyhow

    I had this weird thing happening to me

    This works
    IF INSTR(g$, "DUBLIN") AND CheckEx(g$) OR INSTR(h$, "DUBLIN") AND CheckEx(h$) OR INSTR(i$, "DUBLIN") AND CheckEx(i$) THEN

    This doesn't
    IF ( INSTR(g$, "DUBLIN") AND CheckEx(g$) ) OR ( INSTR(h$, "DUBLIN") AND CheckEx(h$) ) OR ( INSTR(i$, "DUBLIN") AND CheckEx(i$) ) THEN

    note the brackets - The second one is techinally correct
    but for some reason
    in the INSTR(h$,"DUBLIN") returns 8
    and CheckEx(h$) return 1

    and the statement (INSTR(h$, "DUBLIN") AND CheckEx(h$) ) returns 0

    any ideas why??

    by any chance is it trying to 'AND' (as in bitwise) 100+001 = 000
    and in the first one it is a Logical AND??


Comments

  • Registered Users Posts: 437 ✭✭Spunj


    Instr doesn't return a true or false (0 or 1), it returns the position at which it found the searchstring or 0 if it didn't find. You need a boolean value here so see if it exists by checking that it was found anywhere:

    Your code should (using the 2nd sample) look more like :
    OR ( INSTR(h$, "DUBLIN") > 0 AND CheckEx(h$) )
    assuming CheckEx returns true or false.

    That way you are doing boolean logic and not trying to do an AND between 0 and 8 :)


  • Registered Users Posts: 6,240 ✭✭✭hussey


    Originally posted by Spunj

    Your code should (using the 2nd sample) look more like :
    OR ( INSTR(h$, "DUBLIN") > 0 AND CheckEx(h$) )
    assuming CheckEx returns true or false.

    bangs head on table, oh bloody course!!
    stupid me

    Thanks
    its odvious now


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Maybe.

    Funny. Most languages (including most versions of basic) give AND a higher precedence than OR, so the two would be exactly the same.

    If PowerBasic doesn't have this then it's possible that the flaw is as you describe.

    The key to using bitwise And and Or as logical operators is to cast results to boolean. In languages that use the same operators for bitwise and boolean mathematics generally true is the same as -1 (rather than 1 as is common in other languages). VB would be an example of this.

    The reason being that on a two's complement machine then if you treat 0 (zero) as false then:

    The machine sees: NOT(0) = -1
    Boole says: NOT false = True.

    The machine sees: X And -1 = X
    Boole says: X And True = X

    The machine sees: X OR -1 = -1
    Boole says: X OR True = True.

    The machine sees X XOR -1 = NOT X
    Boole says: X XOR True = NOT X

    Hence -1 is what we want.

    Cast the result of each CheckEx to Boolean and you should be ok.

    If CheckEx is your own you could make it return -1 rather than 1.

    If powerbasic doesn't has a boolean cast (like CBool in VB) then you can create one with a function that goes something like this (I'm using VB syntax, you may have to adopt it):


  • Registered Users Posts: 6,240 ✭✭✭hussey


    in power basic
    AND has higher precidence over OR

    I knew the code was teh same, but I wanted the brackets so it would be more clearer

    I had CheckEx(x$) return 1 if true and 0 if false


  • Registered Users Posts: 437 ✭✭Spunj


    Aye Tallesin, but he had already said CheckEx returns 1, so the abs value of the vbTrue doesn't matter :)

    Edit:
    It might, silly me, nevermind...been away from vb for too long :P


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Argh, simultaneous post.

    That matches my point about casting one of them to boolean (>0 casts to bool for any positive number, <>0 casts for any number).


  • Registered Users Posts: 437 ✭✭Spunj


    hehe..at least the problem is solved, thats the main thing!


Advertisement