I think I have this in my head without ever looking it up (but I wouldn't* expect anyone else to know it):
Suppose x is the input and y is the output.
set y to 0
take x and modulo by 2 (don't set x yet), AND y with 0x1
divide x by two and throw away the decimal remainder and set x to this result.
Left shift y by 2.
repeat steps 2, 3 and 4 until x becomes 0. Result is in y. See edit**.
Although I don't see the use for this especially if the language or libraries can do it for you.
EDIT: I thought about this for a few hours and realized this is incorrect because the highest bit of y is the lowest bit of x. The correct algorithm is:
Get an integer i and set it to 1. Also set ints j and y to 0.
Take (x modulo (2 RIGHT SHIFT i)) divided by i, truncating the remainder, and set it to z.
Set y to y OR (z RIGHT SHIFT j).
Multiply i by 2. Increase j by 1.
Repeat steps 2 and 3 and 4 until i is greater than x.
*the original comment had "would" by mistake! oops.
**I guess if you admit you made a mistake in your algorithm during a job interview you are more likely to get hired for being honest.
// , βIt is not so important to be serious as it is to be serious about the important things. The monkey wears an expression of seriousness... but the monkey is serious because he itches."(No/No)
I think I have this in my head without ever looking it up (but I wouldn't* expect anyone else to know it):
Suppose x is the input and y is the output.
set y to 0take x and modulo by 2 (don't set x yet), AND y with 0x1divide x by two and throw away the decimal remainder and set x to this result.Left shift y by 2.repeat steps 2, 3 and 4 until x becomes 0. Result is in y.See edit**.Although I don't see the use for this especially if the language or libraries can do it for you.
EDIT: I thought about this for a few hours and realized this is incorrect because the highest bit of y is the lowest bit of x. The correct algorithm is:
*the original comment had "would" by mistake! oops.
**I guess if you admit you made a mistake in your algorithm during a job interview you are more likely to get hired for being honest.
What planet are you from, and can I visit?
It's taken me a long, long time to find companies that want honesty and behave like they want it.