Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef unsigned int uint32;
- typedef unsigned long long uint64;
- uint64 nearest_power_of_2(uint32 n);
- int main(int argc, char* argv[])
- {
- if (argc < 2) {
- printf("usage %s integer\n", argv[0]);
- return -1;
- }
- uint32 n = atoi(argv[1]);
- printf("Input: %u\nNearest power of 2: %llu\n", n,
- nearest_power_of_2(n));
- return 0;
- }
- uint64
- nearest_power_of_2(uint32 n)
- {
- uint32 i;
- if (n == 0)
- return 0;
- for (i = 31; i > 0; i--) {
- if (n > (1UL << i))
- return 1UL << (i + 1);
- }
- return n;
- }
Add Comment
Please, Sign In to add comment