
John V. answered 11/13/20
Senior Network Engineer
That works well.
Another way to do it is:
- Divide the number by 2.
- Get the integer quotient for the next iteration.
- Get the remainder for the binary digit.
- Repeat the steps until the quotient is equal to 0.
- Then flip the order
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Bit # |
60 | 30 | 15 | 7 | 3 | 1 | 0 | 0 | 0 | Quotient |
--- | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | Remainder |
60 = 00111100
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Bit # |
231 | 115 | 57 | 28 | 14 | 7 | 3 | 1 | 0 | Quotient |
--- | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | Remainder |
231 = 11100111
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Bit # |
159 | 79 | 39 | 19 | 9 | 4 | 2 | 1 | 0 | Quotient |
--- | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | Remainder |
159 = 10011111
Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Bit # |
202 | 101 | 50 | 25 | 12 | 6 | 3 | 1 | 0 | Quotient |
--- | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | Remainder |
202 = 11001010
Whatever method works for you