terciof
 
 
  Joined: 07 Jul 2005 Posts: 3
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Bitwise exclusive OR | 
			 
			
				 Posted: Thu Dec 01, 2005 7:22 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Hi All.
 
 
I Have a var called MedicaoAtual, it will hold only 1 or 0. I declared this as int1, but...:
 
 
When I bitwise exclusive OR with it, the compiler generate a lot of code for just a exclusive OR. But if the var is declared as int only two instructions.
 
 
....................          MedicaoAtual ^= 1; <-- As int1
 
0788:  MOVLW  00
 
0789:  BTFSC  3E.1
 
078A:  MOVLW  01
 
078B:  XORLW  01
 
078C:  MOVWF  78
 
078D:  BTFSC  78.0
 
078E:  GOTO   791
 
078F:  BCF    3E.1
 
0790:  GOTO   792
 
0791:  BSF    3E.1
 
 
...
 
 
I need to verify this variable, if it's 1 PORTB.1 = 1 and PORTB.2 = 0, when it is 0, PORTB.1 = 0 and PORTB.2 = 1.
 
 
So, what is the best solution for this, I try a lot of possibilities like:
 
 
1: 
 
 	  | Code: | 	 		     output_bit(PIN_B1, MedicaoAtual);
 
   output_bit(PIN_B2, input_state(PIN_B1)^1);
 
 | 	  
 
2:
 
 	  | Code: | 	 		     output_bit(PIN_B1, MedicaoAtual);
 
   output_bit(PIN_B2, MedicaoAtual^1);
 
 | 	  
 
3:
 
 	  | Code: | 	 		      if(MedicaoAtual == 0)
 
    {
 
        output_low(PIN_B1);
 
        output_high(PIN_B2);
 
    }
 
    else
 
    {
 
        output_high(PIN_B1);
 
        output_low(PIN_B2);
 
    }
 
 
 
 | 	  
 
All those methods works, but which one is the best one?
 
 
Thanks all.
 
 
Tercio | 
			 
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Dec 01, 2005 7:31 pm     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | Quote: | 	 		  When I bitwise exclusive OR with it, the compiler generate a lot of code for just a exclusive OR. But if the var is declared as int only two instructions. 
 
 
.................... MedicaoAtual ^= 1; <-- As int1 
 
0788: MOVLW 00 
 
0789: BTFSC 3E.1 
 
078A: MOVLW 01 
 
078B: XORLW 01 
 
078C: MOVWF 78 
 
078D: BTFSC 78.0 
 
078E: GOTO 791 
 
078F: BCF 3E.1 
 
0790: GOTO 792 
 
0791: BSF 3E.1  | 	  
 
 
 
You want to invert an int1 variable ?    Try this method:
 
 	  | Code: | 	 		  
 
int1 MedicaoAtual;
 
 
MedicaoAtual = !MedicaoAtual;
 
 | 	  
 
Here is the ASM listing.  It does an XOR with 1 on the specific bit.
 
 	  | Code: | 	 		  
 
0000                00504 ....  
 
0000                00505 .... MedicaoAtual = !MedicaoAtual; 
 
0091 3001       00506 MOVLW  01
 
0092 06AB       00507 XORWF  2B,F | 	 
  | 
			 
		  |