I have the following problem. Here is sample input/output data:
- i: 0, o: 1 (0)
- i: 1, o: 1 (1)
- i: 2, o: 2 (10)
- i: 3, o: 2 (11)
- i: 4, o: 3 (100)
- i: 5, o: 3 (101)
- i: 6, o: 3 (110)
- i: 7, o: 3 (111)
However, that is just using the smallest number of bits to encode the number. I want something slightly different, having a hard time wrapping my head around it. I want the output to be a power of 2.
- i: 0, o: 2 (00)
- i: 1, o: 2 (01)
- i: 2, o: 2 (10)
- i: 3, o: 2 (11)
- i: 4, o: 4 (0100)
- i: 5, o: 4 (0101)
- i: 6, o: 4 (0110)
- i: 7, o: 4 (0111)
- i: 8, o: 4 (1000)
- ...
- i.... o: 8
- ...
- i.... o: 16
- ...
How do I write a function that takes an integer in JavaScript, and returns to me o
for that number? One that is doing it efficiently and not converting things to strings and using random JS helper functions :)