This question has already been answered:
- C ++ column multiplication [closed] 2 responses
Could you tell me how to write a code to multiply two binary numbers? It is necessary to multiply them, therefore, through a translation of 10 ss will not work.
This question has already been answered:
Could you tell me how to write a code to multiply two binary numbers? It is necessary to multiply them, therefore, through a translation of 10 ss will not work.
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
Well, hold on ... I didn’t, as there, do - worked without intermediate arrays, straight with strings ...
string mul(const string& a, const string& b) { string r(a.length()+b.length()-1,'0'); for (int i = a.length()-1; i >= 0; i--) { for (int j = b.length()-1; j >= 0; j--) { r[i+j] += (a[i] != '0' && b[j] != '0'); } } for (int i = r.length()-1; i > 0; i--) { r[i-1] += (r[i]-'0')/2; r[i] = (r[i]-'0')%2 + '0'; } while(r[0] > '1') { r = "0" + r; r[0] += (r[1]-'0')/2; r[1] = (r[1]-'0')%2 + '0'; } return r; } int main(int argc, const char * argv[]) { cout << mul("1001","1001") << endl; cout << mul("101","111") << endl; cout << mul("1000","101") << endl; } a[i] and b[j] are units. I just preferred to compare everywhere with zero. - HarrySource: https://ru.stackoverflow.com/questions/596191/
All Articles