0

I am trying to add two 16 digit binary numbers together. Each binary number will have been entered by the user and saved as: int binaryOne[16] and int binaryTwo[16]. Originally,

int binaryOne[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int binaryTwo[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

are the values declared. Then, the user has the option to change these values using buttons that I implement before the Solution() function. So, once the used gets to the Solution() function, the numbers may be stored as:

int binaryOne[16] = {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1};
int binaryTwo[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};

This is what I have so far:

void Solution() {
  /*
    Rule of binary addition:
    0 + 0 = 0
    1 + 0 = 1
    0 + 1 = 1
    1 + 1 = 1 and carry = 1
   */
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Solution");
   lcd.setCursor(0,2);





    int a[16] = binaryOne[16];
    int b[16] = binaryTwo[16];
    int i = 0;
    int remainder = 0;
    int sum[32];

    while(a[16] != 0 || b[16] != 0){
      sum[i++] = (a[16] % 10 + b[16] % 10 + remainder) % 2;
      remainder = (a[16] % 10 + b[16] % 10 + remainder) / 2; 
      a[16] = a[16] /10;
      b[16] = b[16] / 10;
    }
    if(remainder != 0){
      sum[i++] = remainder;
    }
    i--;
    while(i>=0){
      lcd.print("%d", sum[i--]);
   }
   return 0;
}

I believe I am doing something wrong with calling the arrays since I am renaming them to a[16] and b[16] within this function. I'm also curious, since I only have 16 bits of space on the screen, what if the user enters a binary number such as 1111111111111111 + 1000000000000000 and therefor there is going to be a carry on that first digit? Would it be best just to have an error message?

9
  • Why not just turn the array bits into an int number, and then add them?
    – CrossRoads
    Apr 5 '18 at 17:05
  • int result1 = (a[15]<<15) | (a[14]<<14) | ... (a[2]<<2) | (a[1]}<1) | a[0];
    – CrossRoads
    Apr 5 '18 at 17:06
  • do the same for result2. Probably better if both are unsigned int.
    – CrossRoads
    Apr 5 '18 at 17:07
  • then unsigned long = result1 + result2;
    – CrossRoads
    Apr 5 '18 at 17:07
  • @CrossRoads I need to output the solution as a binary number, sorry if I was not clear.
    – Katie
    Apr 5 '18 at 17:29
1

You're making a mountain out of a molehill.

You're trying to manually manipulate binary digits in an array, but C can already manipulate binary digits. Everything is binary already - you just need to represent it as such.

All you need to do is convert the contents of your array into an actual number, then do simple mathematics on that number. One of the simplest ways of doing that is to shift the bits into a variable:

For instance:

int binaryOne[16] = {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1};
int binaryTwo[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};

uint16_t binOne = 0;
uint16_t binTwo = 0;

for (int i = 0; i < 16; i++) {
    binOne <<= 1;
    binOne |= binaryOne[i];
    binTwo <<= 1;
    binTwo |= binaryTwo[i];
}

Now you can add them together, since they're just numbers:

uint32_t result = binOne + binTwo;

And you can output it as binary:

Serial.println(result, BIN);

If you want to convert back into an array again (why?!) you can just do the opposite as before:

bool resultBin[32];

for (int i = 0; i < 32; i++) {
    resultBin[i] = (result & (1 << (31-i))) ? 1 : 0;
}
4
  • it is probably a school assignment
    – Juraj
    Apr 5 '18 at 18:20
  • I tend to think things are a lot harder than they actually are unfortunately haha. I don't know why I didn't see this easy solution. Thank you!
    – Katie
    Apr 5 '18 at 18:34
  • Read the part of my answer that starts with: "And you can output it as binary..."
    – Majenko
    Apr 5 '18 at 18:48
  • @Majenko Oh duh, it was right there, sorry. I will check your answer as accepted. I really appreciate your help!
    – Katie
    Apr 5 '18 at 18:55
2

A few issues here:

  1. The way you “rename” the arrays is wrong, and furthermore there is no point in doing such renaming.
  2. There is no reason to divide by 10 (ten, in decimal) within your code: as everything you do is binary, the number ten has nothing to do with what are trying to accomplish.
  3. While doing the addition, you forgot to loop over the bits.
  4. You shouldn't return 0 in a function of type void. In fact, you shouldn't return any value.

Fixing those issues should give you something similar to this:

void Solution() {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Solution");
    lcd.setCursor(0, 2);

    // Compute the sum.
    int sum[16];
    int remainder = 0;
    for (int i = 0; i < 16; i++) {
        int s = binaryOne[i] + binaryTwo[i] + remainder;
        sum[i] = s % 2;
        remainder = s / 2;
    }

    // Print it.
    for (int i = 15; i >= 0; i--) {
        lcd.print("%d", sum[i]);
    }
}

Note that there is no point in computing a 32-bit result if you can only print 16 bits: just accept that the computation is done modulo 216, just like a 16-bit ALU would do, or the unsigned int data type in the Arduino.

Of course, the whole idea of doing the computation one bit at a time is highly inefficient. See Majenko's answer for a more idiomatic and efficient way of managing numbers bit-wise in C and C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.