How to XOR?

8 views
Skip to first unread message

Mike

unread,
Apr 27, 2026, 2:04:27 PMApr 27
to jallist
I have this line in my program:

T1CON_TMR1ON = T1CON_TMR1ON ^ 0b0000_0001

which toggles Timer1.  I thought it should result in only two assembly instructions:

MOVLW    1
XORWF    v_t1con,f

But it actually results in five:

;  220       T1CON_TMR1ON = T1CON_TMR1ON ^ 0b0000_0001
             btfss    v_t1con, 0 ; t1con_tmr1on
             goto     l__l718
             bcf      v_t1con, 0 ; t1con_tmr1on
             goto     l__l717
l__l718
             bsf      v_t1con, 0 ; t1con_tmr1on
l__l717

But I  noticed that another part of the code:

ch = ch ^ 0xFF

correctly generates assembly using the XORWF instruction.

Do I have unreasonable expectations?  Why does it work in some situations, but not others?

-Mike

Pinhead

unread,
Apr 28, 2026, 8:54:26 AMApr 28
to jal...@googlegroups.com

Hi,

If you just want to flip the T1CON_TMR1ON bit, you can use

T1CON_TMR1ON = !T1CON_TMR1ON

The ^ operator is useful when you want to flip an entire byte (or word or dword)



Le 27-04-26 à 20:04, 'Mike' via jallist a écrit :
--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jallist/733e5150-89f8-4e25-ad20-f05ea7dbcefdn%40googlegroups.com.

Mike

unread,
Apr 28, 2026, 9:51:58 AMApr 28
to jallist
I tried that.  It still doesn't use the XORWF in assembly.  I'm not saying this is definitely wrong, or awful, I just want to know why the compiler doesn't choose the simpler and shorter code.

Mike

unread,
Apr 28, 2026, 11:40:02 AMApr 28
to jallist
I think it must be a rule in the compiler.  It generates an XORWF when using a regular variable, but doesn't when using a Special Function Register such as T1CON.  It may have been done to avoid the problem with the PORT registers and just implemented as a general rule for all SFR's.
Reply all
Reply to author
Forward
0 new messages