Language
C
Compiler
gcc 13.2.0
Options
Warnings
Compiler Default
no pedantic
Raw compiler options
-lm
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
uint32_t calc(int num) {
union {
double d;
uint64_t u;
} value;
/* 3乗根を求める */
value.d = pow(num, 1.0 / 3.0);
/* 正規化を解除し、整数部分が本来の値になるようにシフトする */
for (int v = (int)value.d; v > 1; v >>= 1) value.u <<= 1;
/* 求めた値の下位52ビット (仮数部) のうち、上位32ビットを取り出す */
return value.u >> 20;
}
int main(void) {
printf("%08" PRIx32 "\n", calc(2));
printf("%08" PRIx32 "\n", calc(311));
return 0;
}
$ gcc prog.c -Wall -Wextra -lm428a2f98
c67178f2
Exit Code:
0