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

You are given a number: 25382
Take the modulo of it to test if it is even or odd: 25382 % 2 = 0 <= ???? ???? ???? ???0
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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);

No comments :

Post a Comment

Comment loud, comment often. But comment on the content!

All comments are filtered through to my email, so your spam will never make it. Unless, of course, you wanted to try injection attacks into my email, which would probably not happen since Blogger just tells me that a comment is awaiting moderation and doesn't bother to tell me what it says. I trust Blogger like that..