Sujet : Re: lun - Lucky Number
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : sci.cryptDate : 16. Mar 2025, 16:55:10
Autres entêtes
Organisation : Fix this later
Message-ID : <vr6s90$232kc$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla Thunderbird
On 16/03/2025 14:57, Marcel Logen wrote:
Richard Heathfield in sci.crypt:
<snip>
#define BIT_QRY(x,i) (((x)[(i)>>3] & (1<<((i)&7)))!=0)
This is actually another C question:
Why does it have to be "(x)" and "(i)" everywhere instead of
"x" and "i"? What kind of "protection" is meant here?
Turn to Number 3 in your songsheets if you will, to this rather simpler macro:
#define BAD_DOUBLE(x) 2*x
and this invocation:
int e = 1;
int f = 29;
int g = BAD_DOUBLE(e+f);
We'd hope for a result of 60, but what we get is 31 because BAD_DOUBLE(e+f) expands to 2*e+f which is 2*1+29, and precedence requires the * to happen before the +.
Parentheses solve the problem:
#define GOOD_DOUBLE(y) 2*(y)
int h = GOOD_DOUBLE(e+f);
which expands to 2*(e+f) and gives us the 60 we were after.
They are not, however, a complete solution:
#define DODGY_DOUBLE(x) (x)+(x)
looks solid enough, but DODGY_DOUBLE(e++)
will expand to (e++)+(e++)
which modifies a value twice between sequence points, and the resultant behaviour is undefined by the rules of the language.
If in doubt, steer well clear of side effects in macros.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within