EvilZone

Programming and Scripting => C - C++ => : D4rkL3on January 09, 2016, 11:02:20 PM

: A little help?
: 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)
: Re: A little help?
: 0pt1musPr1m3 January 09, 2016, 11:53:57 PM
he probably gave you that exercise so that you would fucking learn something.

This is basic shit bruh, go figure it out.
: Re: A little help?
: D4rkL3on January 09, 2016, 11:55:34 PM
he didn't gave to me to solve it myself he doesn't know the solution and he wants to learn it ;)
: Re: A little help?
: Kurajber January 10, 2016, 12:06:50 AM
(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
: Re: A little help?
: D4rkL3on January 10, 2016, 12:17:17 AM
thank you :)
: Re: A little help?
: ArkPhaze January 10, 2016, 10:03:04 AM
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;
}
: Re: A little help?
: Kurajber January 10, 2016, 10:52:45 AM
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.