Bit compare using PTXD_PTAXX

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bit compare using PTXD_PTAXX

1,792 Views
CarlFST60L_3rd
Contributor I
Sorry if this is very obvious, but why cant I do this

Please note, this is just illistrating the exor compare, nothing more. Using S08 processors, 5.9.0 codewarrior.

Code:
if(PTAD_PTAD1 ^ PTAD_PTAD2) dothis();  //If both the same dothis()else dothis1();          //If one bit is different dothis1()

 
If i do somthing like this, it works (dont use the predefined PTAD_PTAD1 directly):

Code:
uchar Temp1, Temp2;if(PTAD_PTAD1) Temp1 = 1;else Temp1 = 0if(PTAD_PTAD2) Temp2 = 1;else Temp2 = 0if(Temp1 ^ Temp2) dothis();else dothis1();

 
I am guessing its due to the way the PTAD_PTAD1 is setup in the header files? Whats the best way to do this without it getting so ugly?

Again, sorry if this is a simple lesson that I should already know.

Thanks,
Carl



Message Edited by CarlFST60L_3rd on 2008-06-10 11:38 AM
Labels (1)
Tags (1)
0 Kudos
4 Replies

545 Views
allawtterb
Contributor IV


CarlFST60L_3rd wrote:
Sorry if this is very obvious, but why cant I do this

Please note, this is just illistrating the exor compare, nothing more. Using S08 processors, 5.9.0 codewarrior.

Code:
if(PTAD_PTAD1 ^ PTAD_PTAD2) dothis();  //If both the same dothis()else dothis1();          //If one bit is different dothis1()


The compiler seems to generate assembly that works correctly for this.  Your comments are backwards, if the bits are the same then XOR is false so the else condition will run.   
0 Kudos

545 Views
CarlFST60L_3rd
Contributor I
Ah, yes, the comments are backwards, was just writing freehand.

I have looked at the code more to narrow it down, my orginal thoughts of the issue were clearly off. the above examples both work, it was the bit I left out causing the problem:

This is the code (Writen free hand to show the idea)

Code:
//main.c//Some global register used for bit flagsbyte FlagsRegister#define MaskSomeFlag 2main{    byte Temp1,Temp2;                             if(FlagsRegister & MaskSomeFlag) Temp1 = 1;    else Temp1 = 0;    if(Temp1 ^ PTAD_PTAD2){...}        //THIS LINE WORKS AS EXPECTED    if((FlagsRegister & MaskSomeFlag) ^ PTAD_PTAD2){...}  //THIS LINE DOES NOT WORK}
Note, when I compilied and tested this, it was strictly one of the other, I just combined them to illistrate.

Now, here is where I see the issue in the non working code:

lda   PTAD 
and   0x04     //Mask out PTA2
lsra
lsra              //move it into position bit 0? This should be at bit 1 for the compare to work!
psha            //Save on stack
lda   FlagsRegister
and   0x02   //Mask out the correct bit from FlagsRegister
tsx             
eor   ,x        //Do the compare with the wrong bit!
pulh
beq   *+54
...

So, is it my code causing it to be out by one? or?

I am pretty sure my code is 'ok' as it works when each bit is tested seperately.
0 Kudos

545 Views
CompilerGuru
NXP Employee
NXP Employee
Sorry, the compiler is correct and the code contains the bug Smiley Wink.

Using ^ as you intend it only works if both sides have a value of 0 or 1.
Obviously an expression like (x & 2) has a value of 0 or 2, and never 1.
So if you really want to do this, code it like
if (((FlagsRegister & MaskSomeFlag) != 0) ^ PTAD_PTAD2)

Note that if you only use the XOR for this, then I would use a != instead,
as this more clearly shows that you intend to do something if the two sides differ.

I usually do not mix booleans and ints, although this of course works as long as the int's happen to
be 0 and 1's only. So in order to test if the second bit of a register has a different value
than the PTAD_PTAD2 bit, I would write something like:
if (((FlagsRegister & 2) != 0) != (PTAD_PTAD2 != 0))

(although I don't think such patterns are that often)

Daniel




0 Kudos

545 Views
CarlFST60L_3rd
Contributor I
Thanks for clearing up the mistake in my logic!

I really though what i was doing made sense, and the compilier looked like it was 'trying' to do (what i thought was) the right thing.

Thanks,
Carl


Message Edited by CarlFST60L_3rd on 2008-06-11 02:57 AM
0 Kudos