CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Bitwise exclusive OR

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
terciof



Joined: 07 Jul 2005
Posts: 3

View user's profile Send private message

Bitwise exclusive OR
PostPosted: Thu Dec 01, 2005 7:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 01, 2005 7:31 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group