Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Exclusive or

Exclusive or

2004-06-19 by David VanHorn

What's the fastest way to get the exclusive or, of four bits in a register?

0000 = 0
0001 = 1
0010 = 1
0011 = 0
0100 = 1
0101 = 0
0110 = 0
0111 = 0
 and so on...

Not for a parity generator, but I think it ends up as the same problem.

Re: [AVR-Chat] Exclusive or

2004-06-19 by Russell Shaw

David VanHorn wrote:
> What's the fastest way to get the exclusive or, of four bits in a register?
> 
> 0000 = 0
> 0001 = 1
> 0010 = 1
> 0011 = 0
> 0100 = 1
> 0101 = 0
> 0110 = 0
> 0111 = 0
>  and so on...
> 
> Not for a parity generator, but I think it ends up as the same problem.

unsigned char i,reg,odd=0;
reg=<???>;
for(i=0;i<8;i++){
   if(reg&1)
     odd=!odd;
   reg>>=1;
}

Re: [AVR-Chat] Exclusive or

2004-06-19 by Bernd Felsche

On Saturday 19 June 2004 11:47, David VanHorn wrote:
> What's the fastest way to get the exclusive or, of four bits in a
> register?

Hardware. :-)

> 0000 = 0
> 0001 = 1
> 0010 = 1
> 0011 = 0
> 0100 = 1
> 0101 = 0
> 0110 = 0
> 0111 = 0
>  and so on...

I hate to be pedantic but are these any four bits; adjacent bits;
low or high nibble?

> Not for a parity generator, but I think it ends up as the same problem.

-- 
/"\ Bernd Felsche - Innovative Reckoning, Perth, Western Australia
\ /  ASCII ribbon campaign | I'm a .signature virus!
 X   against HTML mail     | Copy me into your ~/.signature
/ \  and postings          | to help me spread!

Re: [AVR-Chat] Exclusive or

2004-06-19 by David VanHorn

At 11:59 AM 6/19/2004 +0800, Bernd Felsche wrote:

>On Saturday 19 June 2004 11:47, David VanHorn wrote:
>> What's the fastest way to get the exclusive or, of four bits in a
>> register?
>
>Hardware. :-)

Yeah, well.. In a 2343 to be precise.

>I hate to be pedantic but are these any four bits; adjacent bits;
>low or high nibble?

I can make them any four bits, but at the moment, they are low nybble.

Re: [AVR-Chat] Exclusive or

2004-06-19 by Mark Jordan

On 18 Jun 2004 at 22:47, David VanHorn wrote:

> 
> What's the fastest way to get the exclusive or, of four bits in a register?
> 
> 0000 = 0
> 0001 = 1
> 0010 = 1
> 0011 = 0
> 0100 = 1
> 0101 = 0
> 0110 = 0
> 0111 = 0
>  and so on...
> 
> Not for a parity generator, but I think it ends up as the same problem.
> 
	
	clr		sum
	sbrc		tmp,0
	inc		sum
	sbrc		tmp,1
	inc		sum
	sbrc		tmp,2
	inc		sum
	sbrc		tmp,3
	inc		sum

	Another way:

	clr		sum
	lsr		tmp
	adc		sum, tmp
	lsr		tmp
	lsr		tmp
	adc		sum, tmp

	The XOR of the four 'tmp' bits is in 'sum', bit 0.

	HTH.
	Mark Jordan

Re: [AVR-Chat] Exclusive or

2004-06-19 by David VanHorn

>
>        
>        clr             sum
>        sbrc            tmp,0
>        inc             sum
>        sbrc            tmp,1
>        inc             sum
>        sbrc            tmp,2
>        inc             sum
>        sbrc            tmp,3
>        inc             sum

That's one cycle longer (but smaller code) than the lookup table.

>        Another way:
>
>        clr             sum
>        lsr             tmp
>        adc             sum, tmp
>        lsr             tmp
>        lsr             tmp
>        adc             sum, tmp

Ok, that is better, 6 cycles, and no table.

Re: [AVR-Chat] Exclusive or

2004-06-20 by wagner

David VanHorn wrote:
> What's the fastest way to get the exclusive or, of four bits in a
> register?
>
> 0000 = 0
> 0001 = 1
> 0010 = 1
> 0011 = 0
> 0100 = 1
> 0101 = 0
> 0110 = 0
> 0111 = 0
>  and so on...
>
> Not for a parity generator, but I think it ends up as the same
> problem.


That is a very tricky question.

If you assume literaly "Exclusive Or" for 4 bits, you are stating that when
JUST ONE bit is UP you will have output UP, it means, only the following
combinations will have output UP:

0001
0010
0100
1000

I can think of many fancy ways to verify that, including digital external
and even a jaw dropping 5 resistors tied to 4 output pins to read an analog
voltage via an adc to identify just one output pin up, but not so simply as
4 easy comparisons for 1, 2, 4 and 8.  This could be done in few different
ways, a loop with a binary shifting 4 comparisons, pure 4 comparisons with
fixed values, etc.

Wagner


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.706 / Virus Database: 462 - Release Date: 6/14/2004

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.