Sujet : Re: lun - Lucky Number
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : sci.cryptDate : 15. Mar 2025, 21:04:38
Autres entêtes
Organisation : Fix this later
Message-ID : <vr4mgm$67a0$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 15/03/2025 19:44, Marcel Logen wrote:
Richard Heathfield in sci.crypt:
<snip>
for(i = 0; i < n; i++)
{
for(j = 7; j >= 0; j--)
{
fprintf(stdout, "%d", !!(b[i] & (1 << j)));
^^
What does this do? Is it necessary?
(As you see, I'm not a C programmer. ;-)
Okay. In C, ! is the `not' operator.
0 is false, 1 is true, non-zero is true, but not true is false. So:
x = 6;
printf("%d", x); prints 6.
printf("%d", !x); prints 0 because not non-zero is false.
printf("%d", !!x); prints 1 because not not non-zero is true.
So !!x maps 0 to 0 and everything else to 1. If you're printing bits, it's just the job.
}
putchar('\n');
}
printf("--------\n");
}
>
int main(void)
{
unsigned char t[8] = {31, 41, 59, 26, 53, 58, 97, 32};
unsigned char r[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char l[8] = {0, 0, 0, 0, 0, 0, 0, 0};
>
puts("t first");
dump(t, 8);
puts("now turn it right 90 deg");
tuwer(r, t);
dump(r, 8);
puts("and back again");
tuwel(l, r);
dump(t, 8);
"dump(l, 8);" (?)
Oh. Er... yeah, good spot. Oops. Fortunately, it amounts to the same thing in this case.
return 0;
}
[...]
#define BIT_QRY(x,i) ((x[(i)>>3] & (1<<((i)&7)))!=0)
#define BIT_SET(x,i) (x)[(i)>>3]|=(1<<((i)&7))
#define BIT_CLR(x,i) (x)[(i)>>3]&=(1<<((i)&7))^0xFF
Can you explain this a little bit?
Three extraordinarily useful macros.
x is a pointer to an array of unsigned char.
i is a bit index, so to speak.
Say i is 77, which is
01001101
i >> 3 gives us 00001001, or 9.
i & 7 gives us 00000101, or 5.
So these macros respectively query, set, or clear bit 5 of byte 9 (counting from x).
A somewhat hurried explanation, so feel free to ask further.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within