Menu

complement of a number

2020-12-18
2020-12-27
  • David Stephenson

    Many (PIC)chips support the COMF instruction, but it does not seem to be supported in GCBasic.
    I have been using XOR (to obtain the complement of variable1 and put it in variable2).
    variable2=255 XOR variable1
    This is 3 instructions in ASM

    COMF variable1,W
    MOVWF variable2

    achieves the same with 2 instructions.
    This may not seem like much but I am using it to drive an LCD display with the processor running at 32kHz (LCDs need to reverse the polarity regularly).
    Anyway such is the versatility of GCBasic that you can throw in ASM lines and they will compile (and work).
    I'm not really sure why chips with limited instruction sets include the COMF instruction (as there is an easy workaround).

     
  • mkstevo

    mkstevo - 2020-12-22

    I am easily baffled by boolean logic at the best of times, but doesn't "NOT" achieve the same thing as:

    variable2= 255 XOR variable1
    

    as in:

    Variable2 = NOT variable1
    

    I understand that COMF gives a complementary inversion which should maintain the two's complement value, but would (or does) XOR'ing with 255 keep the signed 'bit'?

    I've never needed to work with signed integers so perhaps that is where my lack of understanding lies.

    I always keep well away from these type of instructions, due to my massive ignorance of them (as shown) so I would be tempted just to use "NOT" having checked first if the value was greater than 127 were I interested in the signed aspect.

    Sorry for being dense. Any explanations may help me understand things a little better.

     
  • David Stephenson

    You're quite correct. It was me being dense, never having used NOT I did not know of its existence.
    And if you try...
    variable2= NOT variable1
    it gives the expected ASM
    COMF variable1,W
    MOVWF variable2

     
  • mkstevo

    mkstevo - 2020-12-23

    Sorry, I didn't mean to suggest anything other than my befuddlement!

     
  • stan cartwright

    stan cartwright - 2020-12-26

    you could say how nice it looked

     
  • David Stephenson

    In my defense there is no mention of AND OR XOR NOT in "bit-wise operations" in the help file.
    They only mention in "variable operations" in one sentence And, Or, Xor and Not operators function both as bitwise and logical operators.
    Almost all of that section deals with their use as Boolean operators.

     
    • Chris Roper

      Chris Roper - 2020-12-27

      Valid observation and a good point Dave.

      Do you have an idea of how best to word it so that others don't get caught out by that?

      Updating help to ensure that others don't get misled as you did is one of the most valuable contributions to GCBASIC that any community member can make.

       
  • David Stephenson

    Thanks Chris from all those who may be misled.
    It is good that the help is kept as helpful as possible as I'm sure that is where newcomers are either lost or pulled into the GCB community.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.