Skip to content

Instantly share code, notes, and snippets.

@GasparVardanyan
Created July 30, 2023 01:15
Show Gist options
  • Save GasparVardanyan/55f10b06361778d2ea725be7f754f83a to your computer and use it in GitHub Desktop.
Save GasparVardanyan/55f10b06361778d2ea725be7f754f83a to your computer and use it in GitHub Desktop.
# include <stdio.h>
# include <inttypes.h>
uint_least16_t binrev (uint_least16_t n)
{
uint_least16_t r = 0;
while (n)
{
r <<= 1;
r |= n & 1;
n >>= 1;
}
return r;
}
int main (void)
{
uint_least16_t n;
scanf ("%" SCNuLEAST16, & n);
uint_least16_t k = 1;
uint_least32_t m = 1;
uint_least8_t p = 0;
_Bool odd = 1;
while (m < n)
{
m += k;
odd = m < n;
k <<= 1;
m += k;
p++;
}
n -= k - 1;
printf ("%" PRIuLEAST64 "\n", ((uint_least64_t) n << p) + binrev (n >> odd));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment