View previous topic :: View next topic |
Author |
Message |
guest Guest
|
XOR byte operator |
Posted: Fri Oct 08, 2004 9:00 am |
|
|
Does anyone know what the byte operator is for a XOR operation?
Regards,
Darren |
|
|
Ttelmah Guest
|
Re: XOR byte operator |
Posted: Fri Oct 08, 2004 9:19 am |
|
|
guest wrote: | Does anyone know what the byte operator is for a XOR operation?
Regards,
Darren |
^
Best Wishes |
|
|
guest Guest
|
|
Posted: Fri Oct 08, 2004 9:33 am |
|
|
Isn't that a bit-wise operator. I want to XOR two bytes together as a basic checksum.
i.e.
Code: |
01011011
XOR 10011001
--------------
11000010
|
Any other thoughts?
Thanks
Darren. |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 08, 2004 10:32 am |
|
|
guest wrote: | Isn't that a bit-wise operator. I want to XOR two bytes together as a basic checksum.
i.e.
Code: |
01011011
XOR 10011001
--------------
11000010
|
Any other thoughts?
Thanks
Darren. |
That is what the operator does. an XOR of each bit in the byte with the corresponding bit in the other byte. It is 'bitwise', because it does this, rather than treating the whole byte as a 'logical' entity.
Best Wishes |
|
|
jamesjl
Joined: 22 Sep 2003 Posts: 52 Location: UK
|
|
Posted: Fri Oct 08, 2004 10:49 am |
|
|
Great,
Thanks for the help. |
|
|
Squintz
Joined: 08 Dec 2009 Posts: 11
|
|
Posted: Fri Jan 15, 2010 1:43 pm |
|
|
Sorry to bring up such an old thread. Newbie Here!
Is it valid to XOR two inputs to determine an output? Below is what I am trying to do. Basically I want to compare a Switch to an LED and if they are different I want to toggle an output. For some reason I never see the output go High. It constantly stays low. What am I doing wrong?
Code: |
#include <12F675.h>
#fuses INTRC_IO //Inernal RC Oscillator
#fuses NOWDT //No Watchdog Timer
#fuses NOBROWNOUT //No Brownout Detection
#fuses NOMCLR //No Master Clear Reset. Used for I/O Instead
#use delay(clock=31000) //
void main(void)
{
while(1)
{
// if the Switch and the LED are opposite then toggle the power
if(input(PIN_A3)^input(PIN_A2)) // XOR Switch to LED
{
//Turn the unit on
output_low(PIN_A5); // Simulate power button press
delay_ms(500); // Wait with button pressed...
output_float(PIN_A5); // Simulate power button release
}else{
output_float(PIN_A5);
}
delay_ms(200); // Wait before checking again.
}
} |
|
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Fri Jan 15, 2010 2:37 pm |
|
|
^ is a bitwise operator, not a logical operator.
You could always try:
Code: | int1 a=0;
int1 b=0;
int1 c=0; |
Code: | a=input(pin_a3);
b=input(pin_a2);
c=((!a)&&b)||(a&&(!b));
if(c)
.
.
. |
Rohit |
|
|
Squintz
Joined: 08 Dec 2009 Posts: 11
|
|
Posted: Fri Jan 15, 2010 3:19 pm |
|
|
I actually got it to work pretty much exactly how it is but I changed output_float to output_high. It was executing the code I just was not seeing it on my oscope because float essentially disconnect the output pin. I thought float was the same as high but I guess I was wrong.
Thanks for the alternative solution. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Jan 15, 2010 3:43 pm |
|
|
Quote: | ^ is a bitwise operator, not a logical operator. | Applying ^ to int1 respectively bit variables achieves a "logical" XOR. In so far, bitwise operator function
includes the logical. Applying a logical operator to variables larger than one bit performs an implicite (var !=0)
before the logical operation.
There have been however issues, at least with CCS V3, where bits in structures have been erroneously
bitwise processed in logical operations.
In the present case, using ^saves a few machine instructions, although the generated code is very similar to
the suggested more long-winded construction.
Code: | if(input(PIN_A3)^input(PIN_A2)) |
|
|
|
|