Im trying to create the following loop :
int temp=0x07;
if(left sensor detects){
byteTx((temp)^(1<<0)); //(toggle bit 0)
}
if(right sensor detects){
byteTx((temp)^(1<<2)); //(toggle bit 2)
}
if(front sensor left || front sensor right detects){
byteTx((temp)^(1<<1)); //(toggle bit 1)
}
However, every other if statement will toggle the correct value but reset the others (because temp is 0x07).
Question : How can I make sure that I only toggle the bit needed, and keep the others unchanged even if they were toggled by the other if statements. Basically I want to store the outputs and use that instead of temp.
Thanks for the help, if you need more info let me know.
edit
Thanks for the responses but im not asking how to toggle, I’m asking how can I make sure that the value toggled is consistent between the IF statements.
0x07 will keep resetting the other bits that are toggled in other IF statements, I want to make sure that the IF statements do not reset the other bits.
Example
0000 0111 ^(1<<0) means the output is 0000 0110
but then if for the other IF statement :
0000 0111 ^(1<<2) means the output is 0000 0011
as you see the result is 0011, which will turn the 0 bit in the first statement back to 1.
how can I prevent that ?
if you want to temp to hold the toggled bits after toggleing, you must modify it, by writing the new state back.
soemthing like
at the end of the loop,
tempwill have all the toggled bits.