Re: 8 Bit Random Numbers



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

.



Relevant Pages

  • Re: Some astro questions
    ... >> time is discrete and comes in units of days. ... > for zero seconds. ... continuum depending upon the relative motion. ...
    (sci.physics)
  • Re: The annotated annotated annotated C standard
    ... outside the Standard in order to achieve a platform-specific goal. ... mathematics found itself ... it is true that there are some cases where division by zero ...
    (comp.programming)
  • Re: Variation on the 5v -> 12v newbie question...
    ... >> be even slower to lose its energy, because using the diode will only ... >> allow a small voltage across the inductor during turn-off. ... it is headed for a value on the other side of zero. ...
    (sci.electronics.basics)
  • Re: Cardinality question
    ... school, I was taught that zero was a place holder for a number, therefore was not a number itself, but rather was an artifact of positional notation of numerals. ... "...how an individual invents a new way of giving order to data now all assembled must here remain inscrutable and may be permanently so... ... Almost always the men who achieve these fundamental inventions of new paradigm have either been very young or very new to the field whose paradigm they change... ...
    (sci.math)
  • Re: iTunes question for JMS
    ... Guskin of rec.arts.sf.tv.babylon5.moderated make plain: ... or contest show) I'm certainly in no position to judge their ...
    (rec.arts.sf.tv.babylon5.moderated)

Quantcast