Talk:Permuted congruential generator

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

I really like the rewrite of {20:55, 3 November 2017}‎, and its followup minor erits. good cleanup and cross referencing. Ollj (talk) 08:48, 27 July 2018 (UTC)[reply]

unary negation of unsigned integer????[edit]

in the beginning of the Example Code section, there is the line
return x >> r | x << (-r & 31);
in which the unary negation operator "-" is applied to the unsigned integer "r". What the hack is that going to produce?? — Preceding unsigned comment added by 46.114.104.78 (talk) 18:04, 24 May 2020 (UTC)[reply]

This is actually well defined in the C language. Approximately: pretend r is a signed integer with the same bit pattern, negate that number, then the result is the unsigned integer with the same bit pattern. In this case the whole line of code you quote does a bitwise rotate of a 32 bit number. Yes, it is ugly, but low level code of this kind usually is. 110.145.147.110 (talk) 02:16, 14 October 2021 (UTC)[reply]

"Negate that number": and how do you do that? While most computers use two's-complement arithmetic, there are some good reasons to use one's-complement arithmetic in the hardware. A description of an algorithm must be machine-independent and language-independent, otherwise the description is not unambiguous. Algorithms relying on C language should be ignored as poorly-specified. I am mindful that some other algorithms are still defined in Fortran (see ACORN (PRNG)#Code example) or Algol, but they should be redefined too. The minus sign can only be used for decimal integer arithmetic, as is done by human beings and as was done in early IBM computers, but few computers today still implement such algorithms in hardware. David Spector (talk) 15:30, 6 May 2022 (UTC)[reply]
In Rust this would just be `x.rotate_right(r)`. And apparently in C++20 there's `rotr`. So this comes down to a question of whether we think code samples should force the reader to learn conventions that were peculiar to C. Myas012 (talk) 07:02, 8 February 2024 (UTC)[reply]