0

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.

15
  • 2
    Post the code that you have tried so we can see where you've gone wrong. Nobody will just give you an implementation. Commented Jan 29, 2017 at 20:45
  • 2
    Are there any integer variables that are not in binary form? Commented Jan 29, 2017 at 20:45
  • Show us your code that produces (a) your "long int number which is in binary form", (b) your "binary number" you wish to subtract or add, (c) your expected output, (d) your actual output. Commented Jan 29, 2017 at 20:45
  • It is not clear what you want to do. All numbers are stored in binary form. Commented Jan 29, 2017 at 20:46
  • If you overflow, then you overflow. The implementation can't contain such a number in a long int. You should stick to whatever big number implementation you had. Commented Jan 29, 2017 at 20:46

2 Answers 2

1

Store as chars it what you want to do I think

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

int main() {
  long int binary1;
  long int binary2 = 0001;
  int i = 0, rem = 0;
  char sum[20];
  printf("Enter binary number: ");
  scanf("%ld", &binary1);

  while (binary1 != 0 || binary2 != 0) {
    sum[i++]=(binary1 % 10 + binary2 % 10 + rem) % 2 > 0 ? '1' : '0';
    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("%c", sum[i--]);

  printf("\n");

  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

your problem is that

long int binary1=1011;

will load your variable with 1011 dec which is 1111110011 bin you can use standard addition operator but different load/print operations. If you want to stick to your DCB (decimal coded binary) encoding (similar to BCD binary coded decimal) then you need to rewrite all math operation. I think simpler would be this:

long int bin2dec(long int bin) // DCB -> binary
 {
 long int x,m;
 for (m=1;m<bin;m*=10);
 for (x=0;m;m/=10)
  {
  x<<=1;
  x+=bin/m;
  bin%=m;
  }
 return x;
 }

long int dec2bin(long int dec) // binary -> DCB
 {
 long int x,m;
 for (m=1;m<dec;m<<=1);
 for (x=0;m;m>>=1)
  {
  x*=10;
  x+=dec/m;
  dec&=m-1;
  }
 return x;
 }

void main()
 {
 long int b1=1011;
 long int b2=   1;
 long int b3= 100;
 long int sum;
 sum = dec2bin(bin2dec(b1)+bin2dec(b2)+bin2dec(b3));
 // here print the sum or whatever
 }

The point is to convert DCB into standard binary representation before math operations and then convert the result back to DCB for printing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.