Integer Math – Converting to Binary

Yes, I realize this is a beginning digital logic concept, but I need somewhere to write this so I don’t forget again.

Unsigned Integer to Binary and Back

  1. You are given a number: 25382
    Take the modulo of it to test if it is even or odd: 25382 % 2 = 0 <= ???? ???? ???? ???0
  2. Divide by two to get a new integer: 25382 / 2 = 12691
    Take the modulo of the new number to test if it is even or odd: 12691 % 2 = 1 <= ???? ???? ???? ??10
  3. Divide by two to get a new integer: 12691 / 2 = 6345
    Take the modulo of the new number to test if it is even or odd: 6345 % 2 = 1 <= ???? ???? ???? ?110
  4. Divide by two to get a new integer: 6345 / 2 = 3172
    Take the modulo of the new number to test if it is even or odd: 3172 % 2 = 0 <= ???? ???? ???? 0110
  5. Divide by two to get a new integer: 3172 / 2 = 1586
    Take the modulo of the new number to test if it is even or odd: 1586 % 2 = 0 <= ???? ???? ???0 0110
  6. Divide by two to get a new integer: 1586 / 2 = 793
    Take the modulo of the new number to test if it is even or odd: 793 % 2 = 1 <= ???? ???? ??10 0110
  7. Divide by two to get a new integer: 793 / 2 = 396
    Take the modulo of the new number to test if it is even or odd: 396 % 2 = 0 <= ???? ???? ?010 0110
  8. Divide by two to get a new integer: 396 / 2 = 198
    Take the modulo of the new number to test if it is even or odd: 198 % 2 = 0 <= ???? ???? 0010 0110
  9. Divide by two to get a new integer: 198 / 2 = 99
    Take the modulo of the new number to test if it is even or odd: 99 % 2 = 1 <= ???? ???1 0010 0110
  10. Divide by two to get a new integer: 99 / 2 = 49
    Take the modulo of the new number to test if it is even or odd: 49 % 2 = 1 <= ???? ??11 0010 0110
  11. Divide by two to get a new integer: 49 / 2 = 24
    Take the modulo of the new number to test if it is even or odd: 24 % 2 = 0 <= ???? ?011 0010 0110
  12. Divide by two to get a new integer: 24 / 2 = 12
    Take the modulo of the new number to test if it is even or odd: 12 % 2 = 0 <= ???? 0011 0010 0110
  13. Divide by two to get a new integer: 12 / 2 = 6
    Take the modulo of the new number to test if it is even or odd: 6 % 2 = 0 <= ???0 0011 0010 0110
  14. Divide by two to get a new integer: 6 / 2 = 3
    Take the modulo of the new number to test if it is even or odd: 3 % 2 = 1 <= ??10 0011 0010 0110
  15. Divide by two to get a new integer: 3 / 2 = 1
    Take the modulo of the new number to test if it is even or odd: 1 % 2 = 1 <= ?110 0011 0010 0110
  16. Divide by two to get a new integer: 1 / 2 = 0
    Take the modulo of the new number to test if it is even or odd: 0 % 2 = 0 <= 0110 0011 0010 0110

25382 = 0110 0011 0010 0110

With a bit of programming, this can be highly consolidated:

Note however that the binary value’s lowest-order bit is expressed on the right-hand side whereas an array’s lowest-order element is expressed on the left-hand side. This will result in the direct output of the array being displayed as the reverse of the binary string it represents.

Thus, we will need to parse through the array in reverse order when using its values to either print to the standard output or convert back into the regular integer:

Leave a Reply

Your email address will not be published. Required fields are marked *