Re: 8 Bit Random Numbers
- From: Nobody <nobody@xxxxxxxxxxx>
- Date: Sat, 07 Feb 2009 15:03:41 +0000
On Fri, 06 Feb 2009 09:11:19 +0000, Jasen Betts wrote:
swap(a[x], a[y])
[snip]
A word of caution: do not use the XOR trick for the swap();
this one?
x^=y;
y^=x;
x^=y;
it fails if x == y.
works here.
But we're not swapping x and y, we're swapping a[x] and a[y], i.e.:
a[x]^=a[y];
a[y]^=a[x];
a[x]^=a[y];
This fails if x == y, i.e. if a[x] and a[y] are the same array element;
the new value will always be zero.
The above code might make it obvious, but consider:
#define SWAP(x,y) do { x^=y; y^=x; x^=y; } while (0)
...
SWAP(a[x], a[y]);
This particular flaw was a runner-up in the 2007 Underhanded C Contest:
http://underhanded.xcott.com/?page_id=9
.
- References:
- Re: 8 Bit Random Numbers
- From: Nobody
- Re: 8 Bit Random Numbers
- From: Jasen Betts
- Re: 8 Bit Random Numbers
- Prev by Date: Re: Effect of the supply internal resistance
- Next by Date: Re: Transformer Current
- Previous by thread: Re: 8 Bit Random Numbers
- Next by thread: Re: 8 Bit Random Numbers
- Index(es):
Relevant Pages
|