Home c++ How to multiply two binary numbers in module 2, which are stored...

# How to multiply two binary numbers in module 2, which are stored as rows

Author

Date

Category

Example:
In zero position is stored a senior discharge of a binary number. Result Number consisting of numbers {0.1}.

``````string Mulbinary (Const String & Amp; Left, Const String & Amp; Right)
{
Vector & lt; int & gt; RES;
INT LEN = left.length () + right.length ();
Res.Resize (LEN);
int k = 0;
for (int i = right.length () - 1; i & gt; = 0; i--)
{
for (int j = left.length () - 1; j & gt; = 0; j--)
{
res [k] ^ = ((left [i] - '0') * (Right [j] - '0'));
}
k ++;
}
String T;
for (int i = 0; i & lt; res.size (); i ++)
{
T.Push_Back (Res [i] + '0');
}
RETURN T;
}
``````

But it turns out, it considers it wrong, that is, the answer does not coincide with the fact that in the picture. 1. 1101×1110 = 10110110

Standard method for storing long numbers: in reverse order, i.e. The element with the number 0 stores the younger discharge. This removes a lot of problems.

Multiplication function by 2 (shift)

``````string shift (const string & amp; num) {
Return "0" + Num;
}
``````

Addition of two long binary numbers

``````String Add (Const String & Amp; Left, Const String & Amp; Right) {
STRING TEMP = "";
String A, B;
int w = 0, d;
// A long number, b - short
if (left.length () & lt; right.length ()) {
Temp = Right; a = left;
}
ELSE {
a = right; temp = left;
}
For (int i = 0; i & lt; temp.length (); i ++) {
If (I & LT; A.Length ()) {
d = (temp [i] - '0') + (a [i] - '0') + w;
}
ELSE {
d = (temp [i] - '0') + w;
}
TEMP [i] = (char) (D% 2 + '0');
w = d / 2;
}
if (w == 1) {temp = temp + '1'; }
RETURN TEMP;
}
``````

Multiplication algorithm. At each step we move one factory on 1 position and add to the result.

``````string Mulbinary (Const String & Amp; Left, Const String & Amp; Right)
{
String Temp;
String a = left;
If (Right  == '0') {
temp = "0";
}
ELSE {
temp = left;
}
for (int i = 1; i & lt; right.length (); i ++)
{
a = shift (a);
If (Right [i] == '1') {
}
}
RETURN TEMP;
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.