Menu

Problem with the NOT operator and byte variables ?

2015-03-18
2015-03-19
  • Jacques Nilo

    Jacques Nilo - 2015-03-18

    I am running into a problem using the NOT/! operator with byte variables. The program is as follows:

    one=1
    zero=0
    wait 1 s
    HSerPrint "one and (not zero)"
    HSerPrintCRLF
    hserprint one & (!zero)
    HSerPrintCRLF
    HSerPrint "one or (not one)"
    HSerPrintCRLF
    HSerPrint one | (!one)
    HSerPrintCRLF
    HSerPrint "one xor (not one)"
    HSerPrintCRLF
    HSerPrint one # (!one)
    HSerPrintCRLF
    HSerPrint "not one"
    HSerPrintCRLF
    HSerPrint !one
    HSerPrintCRLF
    HSerPrint "not zero"
    HSerPrintCRLF
    HSerPrint !zero
    

    When one and zero variables are declared as bits the results of logical operations are ok even though !one and !zero show up as 254 and 255:

    one and (not zero)
    1
    one or (not one)
    1
    one xor (not one)
    0
    not one
    254                    <---- ????
    not zero
    255                    <---- ????
    

    When one and zero variables are declared as byte (default) some of the results of the same logical operations are wrong:

    one and (not zero)
    1
    one or (not one)        
    255                    <---- Wrong
    one xor (not one)
    255                    <---- Wrong
    not one
    254
    not zero
    255
    

    Does it mean that the NOT operator cannot be used as a logical operator on byte variables (AND, OR, XOR are working ok on both bit & byte variables) ?
    Jacques

     
  • Anobium

    Anobium - 2015-03-18

    Can you confirm your microprocessor type?

    Thank you.

     
  • Jacques Nilo

    Jacques Nilo - 2015-03-18

    16F690

     
  • Anobium

    Anobium - 2015-03-19

    I had the same question a year or so ago. I just cannot find Hugh's response but I was convinced by the response re the treatment of operators.

    However, something is not right. I am getting the same results as you on PIC but different results on AVR.

    @Hugh. Your view please.

     

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.