Author Topic: A little help?  (Read 783 times)

0 Members and 1 Guest are viewing this topic.

Offline D4rkL3on

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 0
    • View Profile
A little help?
« on: 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)

Offline 0pt1musPr1m3

  • EZ's Asshole
  • Peasant
  • *
  • Posts: 89
  • Cookies: 90
  • Certified Asshole
    • View Profile
Re: A little help?
« Reply #1 on: 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.
Don't measure yourself by what you have accomplished, but by what you should have accomplished with your ability.

Offline D4rkL3on

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 0
    • View Profile
Re: A little help?
« Reply #2 on: 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 ;)

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: A little help?
« Reply #3 on: 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
0000010100100000

Offline D4rkL3on

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 0
    • View Profile
Re: A little help?
« Reply #4 on: January 10, 2016, 12:17:17 am »
thank you :)

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: A little help?
« Reply #5 on: January 10, 2016, 10:03:04 am »
You don't need to use log for this:
Code: [Select]
#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;
}
« Last Edit: January 10, 2016, 10:04:10 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Kurajber

  • Serf
  • *
  • Posts: 43
  • Cookies: 7
  • Don't Drink and Root
    • View Profile
Re: A little help?
« Reply #6 on: 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.
0000010100100000