https://alpacahack.com/daily/challenges/decimal-float-101-0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fail(char const*) __attribute__((noreturn));
void fail(char const* msg) {
puts(msg);
exit(1);
}
void win() {
FILE* fin = fopen("flag.txt", "r");
if (fin == NULL) {
fail("fopen() failed.");
}
char flag[64] = {};
fgets(flag, sizeof flag, fin);
fclose(fin);
printf("Here is the FLAG: %s\n", flag);
}
int main(void) {
setbuf(stdin, NULL);
setbuf(stdout, NULL);
setbuf(stderr, NULL);
_Decimal64 x_dd = 0.1dd;
{
unsigned long x_ul = 0;
memcpy(&x_ul, &x_dd, 8);
printf("hint: %016lX\n", x_ul);
}
unsigned long y_ul = 0;
if (scanf("%lX", &y_ul) != 1) {
fail("an integer in the hexadecimal format is needed.");
}
_Decimal64 y_dd = 0.0dd;
memcpy(&y_dd, &y_ul, 8);
if (x_dd != y_dd) {
fail("their values should be equal.");
}
if (memcmp(&x_dd, &y_dd, 8) == 0) {
fail("their representations should differ.");
}
win();
}
ãĒãã unsigned long ã§å
Ĩåãããã¤ãåã _Decimal64 ãĢãŗããŧããĻãã
_Decimal64 㯠10鞿ĩŽåå°æ°įšæ°ã§ãããã¨ãããã
0.1 ãåŗå¯ãĒå¤ã§čĄ¨įžã§ãããããäģå㯠0.1dd 㨠== ã§åå¤ã ã memcmp() ã§į°ãĒãããŧãŋãæã¤čĄ¨įžãæąããããĻãã
Hint ãčĻãĻãŋã
0x31A0000000000001 ããã_Decimal64 ã¯äģĨä¸ãŽæ§æã§ãã
ã§ã¯ãåĨãŽåŊĸã§ 0.1 ã襨įžããåŋ čĻããã
0x318000000000000A (10鞿ŗã§ãææ°é¨ 396, äģŽæ°é¨ 10)
0x3160000000000064 (ææ°é¨ 395, äģŽæ°é¨ 100)0x318000000000000A