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:

int decVal = 25382;
char binVal[16];
printf("Integer: = Binary: ");
for (int i = 0; i < 16; i++) {
    binVal[i] = (decVal % 2);
    decVal /= 2;
    printf("%d", binVal[i]); 
}

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.

binInt = 0110001100100110;
// = B15 B14 B13 ... B2 B1 B0;

binVal[16] = {0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0};
// = {I0, I1, I2, ... , I13, I14, I15};

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:

// binVal is calculated from for loop above.
// binVal[16] = {0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0};
int decVal;
printf("Binary String: "); 
for (int i = 0; i < 16; i++) {
    if (binVal[16-(i+1)] == 1) {
        decVal += pow(2,i);
    }
    printf("%d",binVal[16-(i+1)]);
}
printf(" = Integer: %d",decVal);

Leave a Reply

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