EvilZone
Programming and Scripting => C - C++ => : D4rkL3on January 09, 2016, 11:02:20 PM
-
Hi a friend give me this exercise to tell him which the solution is but I don't know C :P if anyone can solve this it will be very helpfully :)
To write a program (only using operators and without orders if / for / while etc) to read a positive integer , and displays the nearest larger number that is power of 2. For example, if the user enters 35, program display 64 (after 2 ^ 5 = 32 < 35 < 64 = 2 ^ 6)
-
he probably gave you that exercise so that you would fucking learn something.
This is basic shit bruh, go figure it out.
-
he didn't gave to me to solve it myself he doesn't know the solution and he wants to learn it ;)
-
(only using operators and without orders if / for / while etc)
Not sure why is that.
Anyways, here's how you use logarithms in C:
http://www.codecogs.com/library/computing/c/math.h/log.php
-
thank you :)
-
You don't need to use log for this:
#include <stdio.h>
int main(void)
{
unsigned int v = 35;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
printf("%u\n", -~v);
return 0;
}
-
Interesting code, I'm not sure if you think that this guy can understand bitwise operations or just shared this for others, but, anyway, interesting.