blob: e1898c386b9505841beefc85bf7e2e577e2604e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// BCD
#include"stdio.h"
int toBCD(int x){
if (x < 100){
int a = ((x / 10) << 4);
int b = (x % 10);
return (a + b);
}
else{
fprintf(stderr, "Number greater than 99 passed to toBCD()");
}
}
int fromBCD(int x){
int a = ((x >> 4) * 10);
int b = (x & 0xF);
return (a + b);
}
int main (){
int i = 39;
printf("1. %d\n2. %d\n3. %d\n", i, toBCD(i), fromBCD(toBCD(i)));
return 0;
}
|