DEV Community

Discussion on: You're not worth hiring unless...

Collapse
 
zenulabidin profile image
Ali Sherief • Edited

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.

  1. set y to 0
  2. take x and modulo by 2 (don't set x yet), AND y with 0x1
  3. divide x by two and throw away the decimal remainder and set x to this result.
  4. Left shift y by 2.
  5. 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:

  1. Get an integer i and set it to 1. Also set ints j and y to 0.
  2. Take (x modulo (2 RIGHT SHIFT i)) divided by i, truncating the remainder, and set it to z.
  3. Set y to y OR (z RIGHT SHIFT j).
  4. Multiply i by 2. Increase j by 1.
  5. 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.

Collapse
 
v6 profile image
πŸ¦„N BπŸ›‘ • Edited

...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.