Incorrect value in subtraction of binary numbers using strings

Asked

Viewed 118 times

0

I made this code and it basically gets two numbers in binaries (no separation of spaces between bits), and Performa (at least in the values I tested) the subtraction correctly. I tried to prevent myself if one of the numbers is smaller than the other, adding a bit of zero value or more ahead of the smallest number. However I do not get the right result for these cases.
PS: It is not necessary to worry about the signal, only about the value of the result being correct.

#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

#define BUFFER_SIZE 256

int main (int argv, char* argc[]){
  char num1[BUFFER_SIZE];
  cin >> num1;
  cin.ignore(1);

  char num2[BUFFER_SIZE];
  cin >> num2;
  cin.ignore(1);

  int length1 = strlen(num1);
  int length2 = strlen(num2);
  int length;
  if(length1 > length2){
    length = length1;
    int diference = length1 - length2;
    for(int i=length2; i>=0;i--){
      num2[i+diference] = num1[i];
    }
    //You must collect the garbage
    for(int i=0; i<diference;i++){
      num2[i] = '0';
    }
  }else{
    length = length2;
    int diference = length2 - length1;
    for(int i=length1; i>=0;i--){
      num1[i+diference] = num1[i];
    }
    //You must collect the garbage
    for(int i=0; i<diference;i++){
      num1[i] = '0';
    }
  }

  char res[(length+2)];
  res[(length+1)] = '\0';

  cout <<"===="<<endl;
  cout<< "num1 : " << num1<< endl;
  cout<< "num2 : " << num2<< endl;

  for(int i=length;i>=0;i--){ 
    /*0 - 1*/
    if((num1[i] == '0' && num2[i] == '1')){
      int j = i-1;
      while(num1[j] != '1'){
        j = j - 1;
      }
      num1[j] = '0';
      res[i+1] = '1';
    }
    /*0 - 0*/
    if(num1[i] == '0' && num2[i] == '0'){
      res[i+1] = '0';
    }

    /*1 - 0*/
    if((num1[i] == '1' && num2[i] == '0')){
      res[i+1] = '1';
    }
    /*1 - 1*/
    if(num1[i] == '1' && num2[i] == '1'){
      res[i+1] = '0';
    }
    res[0] = '0';
  }
  cout<< "res : " << res<< endl;
  return 0;
}
  • Your program is too long to do a quick analysis. I believe the best way for you to solve the problem is to use a Debugger. Now if the intention is to have an example of how to do binary arithmetic using C++, use the Std::bitset makes it so much easier.

2 answers

0

If the intention is to have an example of how to perform binary operations, manipulating the bits, using C++, below is a program that uses the Std::bitset. It makes bit manipulation a lot easier.

#include <bitset>
#include <iostream>
#include <tuple>

auto adder(bool a, bool b, bool c0)
{
    auto r = a ^ b ^ c0;
    auto c1 = a & b | a & c0 | b & c0;
    return std::make_tuple(r, c1);
}

template <long unsigned int N>
auto helper(const std::bitset<N>& x, const std::bitset<N>& y, bool c)
{
    auto result = std::bitset<N>();
    for(int i = 0; i < N; ++i) {
        auto tmp = adder(x[i], y[i], c);
        result[i] = std::get<0>(tmp);
        c = std::get<1>(tmp);
    }
    return result;
}

template <long unsigned int N>
auto soma(const std::bitset<N>& x, const std::bitset<N>& y)
{
    return helper(x, y, 0);
}

template <long unsigned int N>
auto sub(const std::bitset<N>& x, const std::bitset<N>& y)
{
    return helper(x, ~y, 1);
}

int main()
{
    auto x = std::bitset<8>("01100011");
    auto y = std::bitset<8>("00011111");

    std::cout << "soma = " << soma(x, y) << std::endl;
    std::cout << "sub  = " << sub(x, y) << std::endl;
    return 0;
}

Note that the summation uses a full sounder and the subtraction is performed via complement to 2.

0

In C++98 you can solve this problem as follows:

#include <iostream>
#include <bitset>
#include <string>


using namespace std;


string bin( unsigned int n )
{
    string bin;

    while( n )
    {
        bin.insert( 0, 1, (n % 2) ? '1' : '0' );
        n /= 2;
    }

    return bin;
}


unsigned int dec( string b )
{
    bitset<8> bits( b );
    return bits.to_ulong();
}


int main( void )
{
    string a;
    string b;
    string c;

    cout << "num1: ";
    cin >> a;

    cout << "num2: ";
    cin >> b;

    c = bin( dec(a) - dec(b) );

    cout << "res: " << c << endl;

    return 0;
}

Test #1:

num1: 11
num2: 1
res: 10

Test #2:

num1: 1010
num2: 11
res: 111

Test #3:

num1: 111
num2: 1
res: 110

Browser other questions tagged

You are not signed in. Login or sign up in order to post.