I have a long int number which is in binary form.
I want to subtract or add another binary number from this number keeping the data type of destination variable same, i.e long int
How can I do it?
I had a solution in which destination variable was an array of int.
what I tried so far
int main() {
long int binary1;
long int binary2 = 0001;
int i = 0, rem = 0;
int sum[20];
printf("Enter binary number");
scanf("%ld", &binary1);
while (binary1 != 0 || binary2 != 0) {
sum[i++] =(binary1 % 10 + binary2 % 10 + rem) % 2;
rem =(binary1 % 10 + binary2 % 10 + rem) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
return 0;
}
Here the result is stored as int. I want to store it in long int because I need to use this sum for the next addition operation.
How can I do the next addition operation where sum is one of the operand? sum here is in int, and another operand is in long int.
long int. You should stick to whatever big number implementation you had.