Incorrect value in subtraction of binary numbers using strings

0

I made this code and it basically gets two numbers in binaries (without separating spaces between bits), and performs (at least in the values in which I tested) the subtraction correctly. I tried to warn 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 correct result for these cases.
PS : You do not need to worry about the signal, only with 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)] = '
#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)] = '%pre%';

  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;
}
'; 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; }
    
asked by anonymous 19.09.2017 / 18:28

2 answers

0

If the intention is to have an example of how to perform binary operations by manipulating bits , using C ++, a program using std::bitset . It makes it easy to manipulate bits.

#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 sum uses a full adder and the subtraction is performed via an add-on of 2.

    
19.09.2017 / 19:52
0

In C ++ 98 you can work around 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
    
19.09.2017 / 20:02