## Interview Question

## Interview Answer

1 Answer

## Add Answers or Comments

To comment on this, Sign In or Sign Up.

1 Answer

To comment on this, Sign In or Sign Up.

Would you like us to review something? Please describe the problem with this {0} and we will look into it.

Your feedback has been sent to the team, and we'll look into it.

If you are not very familiar with the powers of 2, you can write them down, from left to right, before starting the process. eg. 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1

Find the largest power of 2 that is less than the decimal number, then:

1 - write a '1' for that power-of-2's column

2 - subtract that power-of-2 from the number and keep the remainder

3 - check next lower power-of-2 to see if it is less than the remainder

4 - if no write a '0' for that power-of-2's place and goto step 3

5 - if yes, goto step 1

Example:

Starting with the decimal number 209, the largest power-of-2 that is less than 209 is 128. We write a '1' and subtract 128 from 209 leaving 81. The next smaller power-of-2 is 64. 64 is less than 81 so we write a '1', subtract 64 from 81 leaving a remainder of 17. We then check 32 and write a '0', check 16 and write a '1', leaving a remainder of 1. Next is 8, then 4, then 2, taking a '0' for each leaving a '1' for 1. Putting all this together yields a solution of '11010001' binary, which is 0xD1 (hex) and 209 decimal.

I hope this helps.