0

I am having some problems with writing a program for adding, subtracting, multiplying and dividing binary. the major problem i am having is being able to restricting the program to give output of 1s and 0s (the binary part) and also it needs to be able to manipulate the binary if it as decimals in them such as 101.1 or 1.0101. Below is a program i have found online during my research but i dont fully understand how it works as yet to implement my own nor does it capable of computing binary with decimals in them. I do not want to use arrays as it limits the maximum output depending on the bits you put aside to do these calculations.

Looking forward to some potential ideas.

#include<stdio.h>

int main(){

    long int binary1,binary2;
    int i=0,remainder = 0,sum[20];

    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any second binary number: ");
    scanf("%ld",&binary2);

    while(binary1!=0||binary2!=0){
         sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
         remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
         binary1 = binary1/10;
         binary2 = binary2/10;
    }

    if(remainder!=0)
         sum[i++] = remainder;

    --i;
    printf("Sum of two binary numbers: ");
    while(i>=0)
         printf("%d",sum[i--]);

   return 0;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
robin1234
  • 9
  • 1
  • 6
    This might sound shocking, but todays computers handle all of these values in binary... `int a=10; int b=10; int c=a+b;` Voila, there is the binary sum... What you have issues with is [**printing them in binary**](http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) format. (and as Ivaylo pointed out: getting them as input...) – ppeterka Sep 24 '13 at 14:02
  • 2
    This program can not possibly handle decimal binary numbers. It reads its input numbers using `%ld`. Probably you need to read the input as strings using `%s`. – Ivaylo Strandjev Sep 24 '13 at 14:03
  • 1
    `I do not want to use arrays as it limits the maximum output depending on the bits you put aside to do these calculations` This might also be shocking, but the data types here aren't infinite either... – ppeterka Sep 24 '13 at 14:07
  • Take array size 33 for `float` and 65 for `double`. – haccks Sep 24 '13 at 14:09

1 Answers1

0

That code may work if you have fixed-point decimal numbers.

I'd suggest to parse the string to a double number and then use the standard arithmetic. To parse the number you split the string on the "." decimal separator. You parse the integer part from right to left multiplying the ones and the zeroes by growing powers of two, starting from 0. Then you parse the other string (decimal part) multiplying by the inverse of growing powers of two, starting from one.

For example: 110.11001

110 + 11001

Integer, reversed = 0*2^0 + 1*2^1 + 1*2^2 Decimal = 1*2^-1 + 1*2^-2 + 0*2^-3 + 0*2^-4 + 1*2^-5

Qualsiasi
  • 51
  • 2
  • 6