Multiplying two numbers using only the “left shift” operatorCRC computationArithmetic of irrationals and...

How can I portray body horror and still be sensitive to people with disabilities?

How to know if I am a 'Real Developer'

80-bit collision resistence because of 80-bit x87 registers?

How do I handle a blinded enemy which wants to attack someone it's sure is there?

How do I write a maintainable, fast, compile-time bit-mask in C++?

Why is quixotic not Quixotic (a proper adjective)?

Exploding Numbers

How does holding onto an active but un-used credit card affect your ability to get a loan?

Can I legally make a website about boycotting a certain company?

Is there any danger of my neighbor having my wife's signature?

SQL Server 2017 crashes when backing up because filepath is wrong

Can a Hydra make multiple opportunity attacks at once?

Spells that would be effective against a Modern Day army but would NOT destroy a fantasy one

What is the reason behind this musical reference to Pinocchio in the Close Encounters main theme?

Have any astronauts or cosmonauts died in space?

Did the characters in Moving Pictures not know about cameras like Twoflower's?

How can guns be countered by melee combat without raw-ability or exceptional explanations?

Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded

When distributing a Linux kernel driver as source code, what's the difference between Proprietary and GPL license?

What does @ mean in a hostname in DNS configuration?

Is it Safe to Plug an Extension Cord Into a Power Strip?

How bad is a Computer Science course that doesn't teach Design Patterns?

Minimum energy path of a potential energy surface

Is the tritone (A4 / d5) still banned in Roman Catholic music?



Multiplying two numbers using only the “left shift” operator


CRC computationArithmetic of irrationals and the Vedanta behind it..Writing a recurrence in terms of a shift operatorFinding abundant numbers from 1 to 10 million using a sumTime complexity of binary sumWhy does the following formula cycle the bits by shifting the binary representation from left to right?What do bitwise operators look like in 3d?Why do we divide or multiply by 2 when converting binary?Bitwise Operations and the Naming Convention of their OperatorsProof: number of bits in the product of two binary numbers.













4












$begingroup$


At Geeks for Geeks, I came across a question "Add two numbers without using arithmetic operators", which basically states that you can multiply two numbers using the left shift operator. I am confused how that can be accomplished. I am trying to understand the logic here. It states that




We can solve this problem with the shift operator. The idea is based
on the fact that every number can be represented in binary form. And
multiplication with a number is equivalent to multiplication with
powers of 2. Powers of 2 can be obtained using left shift operator.




I dont understand what the above means. I know that left shifting by 1 is like multiplying by 2, and left shifting by 2 is is like multiplying that number by 4. However, I am confused at how I can use left shift to evaluate something like 7 * 3 ?










share|cite|improve this question











$endgroup$








  • 1




    $begingroup$
    $7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
    $endgroup$
    – Steve Kass
    4 hours ago
















4












$begingroup$


At Geeks for Geeks, I came across a question "Add two numbers without using arithmetic operators", which basically states that you can multiply two numbers using the left shift operator. I am confused how that can be accomplished. I am trying to understand the logic here. It states that




We can solve this problem with the shift operator. The idea is based
on the fact that every number can be represented in binary form. And
multiplication with a number is equivalent to multiplication with
powers of 2. Powers of 2 can be obtained using left shift operator.




I dont understand what the above means. I know that left shifting by 1 is like multiplying by 2, and left shifting by 2 is is like multiplying that number by 4. However, I am confused at how I can use left shift to evaluate something like 7 * 3 ?










share|cite|improve this question











$endgroup$








  • 1




    $begingroup$
    $7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
    $endgroup$
    – Steve Kass
    4 hours ago














4












4








4





$begingroup$


At Geeks for Geeks, I came across a question "Add two numbers without using arithmetic operators", which basically states that you can multiply two numbers using the left shift operator. I am confused how that can be accomplished. I am trying to understand the logic here. It states that




We can solve this problem with the shift operator. The idea is based
on the fact that every number can be represented in binary form. And
multiplication with a number is equivalent to multiplication with
powers of 2. Powers of 2 can be obtained using left shift operator.




I dont understand what the above means. I know that left shifting by 1 is like multiplying by 2, and left shifting by 2 is is like multiplying that number by 4. However, I am confused at how I can use left shift to evaluate something like 7 * 3 ?










share|cite|improve this question











$endgroup$




At Geeks for Geeks, I came across a question "Add two numbers without using arithmetic operators", which basically states that you can multiply two numbers using the left shift operator. I am confused how that can be accomplished. I am trying to understand the logic here. It states that




We can solve this problem with the shift operator. The idea is based
on the fact that every number can be represented in binary form. And
multiplication with a number is equivalent to multiplication with
powers of 2. Powers of 2 can be obtained using left shift operator.




I dont understand what the above means. I know that left shifting by 1 is like multiplying by 2, and left shifting by 2 is is like multiplying that number by 4. However, I am confused at how I can use left shift to evaluate something like 7 * 3 ?







number-theory computer-science






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited 3 hours ago







MistyD

















asked 5 hours ago









MistyDMistyD

74861836




74861836








  • 1




    $begingroup$
    $7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
    $endgroup$
    – Steve Kass
    4 hours ago














  • 1




    $begingroup$
    $7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
    $endgroup$
    – Steve Kass
    4 hours ago








1




1




$begingroup$
$7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
$endgroup$
– Steve Kass
4 hours ago




$begingroup$
$7cdot3 = 7cdot(2+1)$, or $7cdot3 = (4+2+1)cdot3$.
$endgroup$
– Steve Kass
4 hours ago










2 Answers
2






active

oldest

votes


















2












$begingroup$

For example, to multiply $7 times 3$, write both in binary :
$$
7 = 111 quad; quad 3 = 11
$$



Now, the point is, imagine doing long multiplication with these numbers :
$$
require{enclose}
begin{array}{}
111 \[-3pt]
underline{times 11 }\[-3pt]
111 \[-3pt]
underline{ 1110} \[-3pt]
underline{10101}(=21)
end{array}
$$



The idea is this : $3$ has ones in both its units and "twos" position, so we shift $7$ by $0$, then $7$ by $1$, and add them up.



Now, imagine doing this for numbers with many zeros. For example , for :
$$
1010 = 10 quad ; quad 11 = 1011
$$



Then :
$$
require{enclose}
begin{array}
1010 \[-3pt]
underline{ times 1011} \[-3pt]
1010 \[-3pt]
10100 \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1010000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which amounts to adding $1010$'s left shifts zero times, one time and three times. Note that these are exactly the positions at which $1011$ has a one.



We will reverse the above and look at it :



$$
require{enclose}
begin{array}
1011 \[-3pt]
underline{ times 1010} \[-3pt]
color{blue}{0000} \[-3pt]
{10110} \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1011000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which is equivalent to adding $1011$ left-shifted exactly once and thrice, which are exactly the positions at which $1010$ has a one.





The above suggests a strategy for multiplying binary numbers $a$ and $b$ :




  • Find the positions at which $b$ has a one. Call these positions $p_1,p_2,...,p_n$.


  • Consider the binary numbers formed by left-shifting $a$ by $p_1, p_2,..,p_n$.


  • Add all these numbers, to get the answer.





Note that this does not involve "hard" multiplication, because the left shift is technically multiplication by powers of $2$ but (as for powers of $10$ in the decimal case) is easily performed, and we are reduced to the far easier operation of addition of the left shifts.



This technique often appears in books that cover quick multiplication techniques, especially by numbers which are close to powers of $2$.





EDIT: For Vakil's $20 times 13$, I will first wrote down what he does, then how he gets the answer :
$$
begin{array}{rrr}
0 && 20 && 13 \
1 && 40 && 6 \
2 && 80 && 3 \
3 && 160 && 1 \[1pt]
end{array}
$$



Then he adds $160+80+20 = 260$, the right answer.



In procedure , this is what is happening :




  • On the first line we write the two numbers, and then indicate it as row number zero.


  • Then, in the next row we double the number in the second entry, divide the third entry by $2$ and round down , and indicate it as row number $1$.


  • We do this until we reach $1$ as the third entry of some row.


  • Now, in the second column, pick all entries such that the corresponding third entry is odd. So we picked $160$ because $1$ is odd, picked $80$ because $3$ is odd, did not pick $40$ as $6$ is even and picked $20$ as $13$ is odd.


  • Add those entries to get the answer.



How does this work? Let us write the same numbers above in binary to see what is happening :
$$
begin{array}{rrr}
0 && 10100 && 1101 \
1 && 101000 && 110 \
2 && 1010000 && 11 \
3 && 10100000 && 1 \[1pt]
end{array}
$$



We add $10100$ because $1101$ ends with $1$. We add $1010000$ because $11$ ends with $1$. We add $10100000$ because $1$ ends with $1$. We do not add $101000$ because $110$ ends with $0$.



Now it is fairly obvious how this algorithm comes to our rescue. The "division by two" step is nothing but "removing the last bit" in binary. Now, we focus on the last bit of the new number, and if this is $1$ we add that entry on the second row, otherwise we leave it. Again we divide by $2$, thus removing the last bit, etc.



I think from here you can figure out how the algorithm works, and how it corresponds to what I wrote earlier.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
    $endgroup$
    – MistyD
    4 hours ago










  • $begingroup$
    Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
    $endgroup$
    – астон вілла олоф мэллбэрг
    3 hours ago










  • $begingroup$
    I just updated the link can you tell me why the author divides by 2 after every iteration
    $endgroup$
    – MistyD
    3 hours ago










  • $begingroup$
    It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago










  • $begingroup$
    Please check the edits. I can add more if required.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago



















3












$begingroup$

I looked at the link, but couldn't find where it says the text you have quoted. Regardless, with what it says & for what you're asking for, i.e., multiplying by using just the shift operator, I believe it means that it works only for multiplying each power of $2$ by left shifting by that power value. You can then use the procedures & code in the link to add the numbers without using arithmetic operators, but with this also using bitwise XOR and AND.



With your example, note that $7 = 2^2 + 2^1 + 2^0$ and $3 = 2^1 + 2^0$. Let's say we start with $7$. Note that $7 times 3$ is $7 times left(2^1 + 2^0right)$. We use the distribution property to get that $7 times 3 = 7 times 2^1 + 7 times 2^0$. For the first term, we can do a left-shift by $1$ bit, the add this to the original value as the second term is already $7$. As such, we get the result to be $left(2^3 + 2^2 + 2^1right) + left(2^2 + 2^1 + 2^0right)$, with this in decimal being $14 + 7 = 21$.



Alternatively, if we started with $3 = 2^1 + 2^0$, we would use that $7 = 2^2 + 2^1 + 2^0$ to do the multiplications & addition to get that $7 times 3 = left(2^3 + 2^2right) + left(2^2 + 2^1right) + left(2^1 + 2^0right) = 21$. In this case, the terms are different, and there are $3$ instead of $2$, but the sum is still the same of course.






share|cite|improve this answer











$endgroup$













  • $begingroup$
    I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
    $endgroup$
    – MistyD
    2 hours ago












  • $begingroup$
    @MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
    $endgroup$
    – John Omielan
    2 hours ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3122152%2fmultiplying-two-numbers-using-only-the-left-shift-operator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2












$begingroup$

For example, to multiply $7 times 3$, write both in binary :
$$
7 = 111 quad; quad 3 = 11
$$



Now, the point is, imagine doing long multiplication with these numbers :
$$
require{enclose}
begin{array}{}
111 \[-3pt]
underline{times 11 }\[-3pt]
111 \[-3pt]
underline{ 1110} \[-3pt]
underline{10101}(=21)
end{array}
$$



The idea is this : $3$ has ones in both its units and "twos" position, so we shift $7$ by $0$, then $7$ by $1$, and add them up.



Now, imagine doing this for numbers with many zeros. For example , for :
$$
1010 = 10 quad ; quad 11 = 1011
$$



Then :
$$
require{enclose}
begin{array}
1010 \[-3pt]
underline{ times 1011} \[-3pt]
1010 \[-3pt]
10100 \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1010000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which amounts to adding $1010$'s left shifts zero times, one time and three times. Note that these are exactly the positions at which $1011$ has a one.



We will reverse the above and look at it :



$$
require{enclose}
begin{array}
1011 \[-3pt]
underline{ times 1010} \[-3pt]
color{blue}{0000} \[-3pt]
{10110} \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1011000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which is equivalent to adding $1011$ left-shifted exactly once and thrice, which are exactly the positions at which $1010$ has a one.





The above suggests a strategy for multiplying binary numbers $a$ and $b$ :




  • Find the positions at which $b$ has a one. Call these positions $p_1,p_2,...,p_n$.


  • Consider the binary numbers formed by left-shifting $a$ by $p_1, p_2,..,p_n$.


  • Add all these numbers, to get the answer.





Note that this does not involve "hard" multiplication, because the left shift is technically multiplication by powers of $2$ but (as for powers of $10$ in the decimal case) is easily performed, and we are reduced to the far easier operation of addition of the left shifts.



This technique often appears in books that cover quick multiplication techniques, especially by numbers which are close to powers of $2$.





EDIT: For Vakil's $20 times 13$, I will first wrote down what he does, then how he gets the answer :
$$
begin{array}{rrr}
0 && 20 && 13 \
1 && 40 && 6 \
2 && 80 && 3 \
3 && 160 && 1 \[1pt]
end{array}
$$



Then he adds $160+80+20 = 260$, the right answer.



In procedure , this is what is happening :




  • On the first line we write the two numbers, and then indicate it as row number zero.


  • Then, in the next row we double the number in the second entry, divide the third entry by $2$ and round down , and indicate it as row number $1$.


  • We do this until we reach $1$ as the third entry of some row.


  • Now, in the second column, pick all entries such that the corresponding third entry is odd. So we picked $160$ because $1$ is odd, picked $80$ because $3$ is odd, did not pick $40$ as $6$ is even and picked $20$ as $13$ is odd.


  • Add those entries to get the answer.



How does this work? Let us write the same numbers above in binary to see what is happening :
$$
begin{array}{rrr}
0 && 10100 && 1101 \
1 && 101000 && 110 \
2 && 1010000 && 11 \
3 && 10100000 && 1 \[1pt]
end{array}
$$



We add $10100$ because $1101$ ends with $1$. We add $1010000$ because $11$ ends with $1$. We add $10100000$ because $1$ ends with $1$. We do not add $101000$ because $110$ ends with $0$.



Now it is fairly obvious how this algorithm comes to our rescue. The "division by two" step is nothing but "removing the last bit" in binary. Now, we focus on the last bit of the new number, and if this is $1$ we add that entry on the second row, otherwise we leave it. Again we divide by $2$, thus removing the last bit, etc.



I think from here you can figure out how the algorithm works, and how it corresponds to what I wrote earlier.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
    $endgroup$
    – MistyD
    4 hours ago










  • $begingroup$
    Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
    $endgroup$
    – астон вілла олоф мэллбэрг
    3 hours ago










  • $begingroup$
    I just updated the link can you tell me why the author divides by 2 after every iteration
    $endgroup$
    – MistyD
    3 hours ago










  • $begingroup$
    It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago










  • $begingroup$
    Please check the edits. I can add more if required.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago
















2












$begingroup$

For example, to multiply $7 times 3$, write both in binary :
$$
7 = 111 quad; quad 3 = 11
$$



Now, the point is, imagine doing long multiplication with these numbers :
$$
require{enclose}
begin{array}{}
111 \[-3pt]
underline{times 11 }\[-3pt]
111 \[-3pt]
underline{ 1110} \[-3pt]
underline{10101}(=21)
end{array}
$$



The idea is this : $3$ has ones in both its units and "twos" position, so we shift $7$ by $0$, then $7$ by $1$, and add them up.



Now, imagine doing this for numbers with many zeros. For example , for :
$$
1010 = 10 quad ; quad 11 = 1011
$$



Then :
$$
require{enclose}
begin{array}
1010 \[-3pt]
underline{ times 1011} \[-3pt]
1010 \[-3pt]
10100 \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1010000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which amounts to adding $1010$'s left shifts zero times, one time and three times. Note that these are exactly the positions at which $1011$ has a one.



We will reverse the above and look at it :



$$
require{enclose}
begin{array}
1011 \[-3pt]
underline{ times 1010} \[-3pt]
color{blue}{0000} \[-3pt]
{10110} \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1011000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which is equivalent to adding $1011$ left-shifted exactly once and thrice, which are exactly the positions at which $1010$ has a one.





The above suggests a strategy for multiplying binary numbers $a$ and $b$ :




  • Find the positions at which $b$ has a one. Call these positions $p_1,p_2,...,p_n$.


  • Consider the binary numbers formed by left-shifting $a$ by $p_1, p_2,..,p_n$.


  • Add all these numbers, to get the answer.





Note that this does not involve "hard" multiplication, because the left shift is technically multiplication by powers of $2$ but (as for powers of $10$ in the decimal case) is easily performed, and we are reduced to the far easier operation of addition of the left shifts.



This technique often appears in books that cover quick multiplication techniques, especially by numbers which are close to powers of $2$.





EDIT: For Vakil's $20 times 13$, I will first wrote down what he does, then how he gets the answer :
$$
begin{array}{rrr}
0 && 20 && 13 \
1 && 40 && 6 \
2 && 80 && 3 \
3 && 160 && 1 \[1pt]
end{array}
$$



Then he adds $160+80+20 = 260$, the right answer.



In procedure , this is what is happening :




  • On the first line we write the two numbers, and then indicate it as row number zero.


  • Then, in the next row we double the number in the second entry, divide the third entry by $2$ and round down , and indicate it as row number $1$.


  • We do this until we reach $1$ as the third entry of some row.


  • Now, in the second column, pick all entries such that the corresponding third entry is odd. So we picked $160$ because $1$ is odd, picked $80$ because $3$ is odd, did not pick $40$ as $6$ is even and picked $20$ as $13$ is odd.


  • Add those entries to get the answer.



How does this work? Let us write the same numbers above in binary to see what is happening :
$$
begin{array}{rrr}
0 && 10100 && 1101 \
1 && 101000 && 110 \
2 && 1010000 && 11 \
3 && 10100000 && 1 \[1pt]
end{array}
$$



We add $10100$ because $1101$ ends with $1$. We add $1010000$ because $11$ ends with $1$. We add $10100000$ because $1$ ends with $1$. We do not add $101000$ because $110$ ends with $0$.



Now it is fairly obvious how this algorithm comes to our rescue. The "division by two" step is nothing but "removing the last bit" in binary. Now, we focus on the last bit of the new number, and if this is $1$ we add that entry on the second row, otherwise we leave it. Again we divide by $2$, thus removing the last bit, etc.



I think from here you can figure out how the algorithm works, and how it corresponds to what I wrote earlier.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
    $endgroup$
    – MistyD
    4 hours ago










  • $begingroup$
    Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
    $endgroup$
    – астон вілла олоф мэллбэрг
    3 hours ago










  • $begingroup$
    I just updated the link can you tell me why the author divides by 2 after every iteration
    $endgroup$
    – MistyD
    3 hours ago










  • $begingroup$
    It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago










  • $begingroup$
    Please check the edits. I can add more if required.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago














2












2








2





$begingroup$

For example, to multiply $7 times 3$, write both in binary :
$$
7 = 111 quad; quad 3 = 11
$$



Now, the point is, imagine doing long multiplication with these numbers :
$$
require{enclose}
begin{array}{}
111 \[-3pt]
underline{times 11 }\[-3pt]
111 \[-3pt]
underline{ 1110} \[-3pt]
underline{10101}(=21)
end{array}
$$



The idea is this : $3$ has ones in both its units and "twos" position, so we shift $7$ by $0$, then $7$ by $1$, and add them up.



Now, imagine doing this for numbers with many zeros. For example , for :
$$
1010 = 10 quad ; quad 11 = 1011
$$



Then :
$$
require{enclose}
begin{array}
1010 \[-3pt]
underline{ times 1011} \[-3pt]
1010 \[-3pt]
10100 \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1010000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which amounts to adding $1010$'s left shifts zero times, one time and three times. Note that these are exactly the positions at which $1011$ has a one.



We will reverse the above and look at it :



$$
require{enclose}
begin{array}
1011 \[-3pt]
underline{ times 1010} \[-3pt]
color{blue}{0000} \[-3pt]
{10110} \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1011000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which is equivalent to adding $1011$ left-shifted exactly once and thrice, which are exactly the positions at which $1010$ has a one.





The above suggests a strategy for multiplying binary numbers $a$ and $b$ :




  • Find the positions at which $b$ has a one. Call these positions $p_1,p_2,...,p_n$.


  • Consider the binary numbers formed by left-shifting $a$ by $p_1, p_2,..,p_n$.


  • Add all these numbers, to get the answer.





Note that this does not involve "hard" multiplication, because the left shift is technically multiplication by powers of $2$ but (as for powers of $10$ in the decimal case) is easily performed, and we are reduced to the far easier operation of addition of the left shifts.



This technique often appears in books that cover quick multiplication techniques, especially by numbers which are close to powers of $2$.





EDIT: For Vakil's $20 times 13$, I will first wrote down what he does, then how he gets the answer :
$$
begin{array}{rrr}
0 && 20 && 13 \
1 && 40 && 6 \
2 && 80 && 3 \
3 && 160 && 1 \[1pt]
end{array}
$$



Then he adds $160+80+20 = 260$, the right answer.



In procedure , this is what is happening :




  • On the first line we write the two numbers, and then indicate it as row number zero.


  • Then, in the next row we double the number in the second entry, divide the third entry by $2$ and round down , and indicate it as row number $1$.


  • We do this until we reach $1$ as the third entry of some row.


  • Now, in the second column, pick all entries such that the corresponding third entry is odd. So we picked $160$ because $1$ is odd, picked $80$ because $3$ is odd, did not pick $40$ as $6$ is even and picked $20$ as $13$ is odd.


  • Add those entries to get the answer.



How does this work? Let us write the same numbers above in binary to see what is happening :
$$
begin{array}{rrr}
0 && 10100 && 1101 \
1 && 101000 && 110 \
2 && 1010000 && 11 \
3 && 10100000 && 1 \[1pt]
end{array}
$$



We add $10100$ because $1101$ ends with $1$. We add $1010000$ because $11$ ends with $1$. We add $10100000$ because $1$ ends with $1$. We do not add $101000$ because $110$ ends with $0$.



Now it is fairly obvious how this algorithm comes to our rescue. The "division by two" step is nothing but "removing the last bit" in binary. Now, we focus on the last bit of the new number, and if this is $1$ we add that entry on the second row, otherwise we leave it. Again we divide by $2$, thus removing the last bit, etc.



I think from here you can figure out how the algorithm works, and how it corresponds to what I wrote earlier.






share|cite|improve this answer











$endgroup$



For example, to multiply $7 times 3$, write both in binary :
$$
7 = 111 quad; quad 3 = 11
$$



Now, the point is, imagine doing long multiplication with these numbers :
$$
require{enclose}
begin{array}{}
111 \[-3pt]
underline{times 11 }\[-3pt]
111 \[-3pt]
underline{ 1110} \[-3pt]
underline{10101}(=21)
end{array}
$$



The idea is this : $3$ has ones in both its units and "twos" position, so we shift $7$ by $0$, then $7$ by $1$, and add them up.



Now, imagine doing this for numbers with many zeros. For example , for :
$$
1010 = 10 quad ; quad 11 = 1011
$$



Then :
$$
require{enclose}
begin{array}
1010 \[-3pt]
underline{ times 1011} \[-3pt]
1010 \[-3pt]
10100 \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1010000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which amounts to adding $1010$'s left shifts zero times, one time and three times. Note that these are exactly the positions at which $1011$ has a one.



We will reverse the above and look at it :



$$
require{enclose}
begin{array}
1011 \[-3pt]
underline{ times 1010} \[-3pt]
color{blue}{0000} \[-3pt]
{10110} \[-3pt]
color{blue}{000000} \[-3pt]
underline{ 1011000} \[-3pt]
underline{ 1101110}
end{array}
$$



Which is equivalent to adding $1011$ left-shifted exactly once and thrice, which are exactly the positions at which $1010$ has a one.





The above suggests a strategy for multiplying binary numbers $a$ and $b$ :




  • Find the positions at which $b$ has a one. Call these positions $p_1,p_2,...,p_n$.


  • Consider the binary numbers formed by left-shifting $a$ by $p_1, p_2,..,p_n$.


  • Add all these numbers, to get the answer.





Note that this does not involve "hard" multiplication, because the left shift is technically multiplication by powers of $2$ but (as for powers of $10$ in the decimal case) is easily performed, and we are reduced to the far easier operation of addition of the left shifts.



This technique often appears in books that cover quick multiplication techniques, especially by numbers which are close to powers of $2$.





EDIT: For Vakil's $20 times 13$, I will first wrote down what he does, then how he gets the answer :
$$
begin{array}{rrr}
0 && 20 && 13 \
1 && 40 && 6 \
2 && 80 && 3 \
3 && 160 && 1 \[1pt]
end{array}
$$



Then he adds $160+80+20 = 260$, the right answer.



In procedure , this is what is happening :




  • On the first line we write the two numbers, and then indicate it as row number zero.


  • Then, in the next row we double the number in the second entry, divide the third entry by $2$ and round down , and indicate it as row number $1$.


  • We do this until we reach $1$ as the third entry of some row.


  • Now, in the second column, pick all entries such that the corresponding third entry is odd. So we picked $160$ because $1$ is odd, picked $80$ because $3$ is odd, did not pick $40$ as $6$ is even and picked $20$ as $13$ is odd.


  • Add those entries to get the answer.



How does this work? Let us write the same numbers above in binary to see what is happening :
$$
begin{array}{rrr}
0 && 10100 && 1101 \
1 && 101000 && 110 \
2 && 1010000 && 11 \
3 && 10100000 && 1 \[1pt]
end{array}
$$



We add $10100$ because $1101$ ends with $1$. We add $1010000$ because $11$ ends with $1$. We add $10100000$ because $1$ ends with $1$. We do not add $101000$ because $110$ ends with $0$.



Now it is fairly obvious how this algorithm comes to our rescue. The "division by two" step is nothing but "removing the last bit" in binary. Now, we focus on the last bit of the new number, and if this is $1$ we add that entry on the second row, otherwise we leave it. Again we divide by $2$, thus removing the last bit, etc.



I think from here you can figure out how the algorithm works, and how it corresponds to what I wrote earlier.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited 2 hours ago

























answered 4 hours ago









астон вілла олоф мэллбэргастон вілла олоф мэллбэрг

38.7k33476




38.7k33476








  • 1




    $begingroup$
    This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
    $endgroup$
    – MistyD
    4 hours ago










  • $begingroup$
    Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
    $endgroup$
    – астон вілла олоф мэллбэрг
    3 hours ago










  • $begingroup$
    I just updated the link can you tell me why the author divides by 2 after every iteration
    $endgroup$
    – MistyD
    3 hours ago










  • $begingroup$
    It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago










  • $begingroup$
    Please check the edits. I can add more if required.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago














  • 1




    $begingroup$
    This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
    $endgroup$
    – MistyD
    4 hours ago










  • $begingroup$
    Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
    $endgroup$
    – астон вілла олоф мэллбэрг
    3 hours ago










  • $begingroup$
    I just updated the link can you tell me why the author divides by 2 after every iteration
    $endgroup$
    – MistyD
    3 hours ago










  • $begingroup$
    It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago










  • $begingroup$
    Please check the edits. I can add more if required.
    $endgroup$
    – астон вілла олоф мэллбэрг
    2 hours ago








1




1




$begingroup$
This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
$endgroup$
– MistyD
4 hours ago




$begingroup$
This definitely makes sense I am going to reflect on this and then mark it as an answer. Your final strategy definitely helped
$endgroup$
– MistyD
4 hours ago












$begingroup$
Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
$endgroup$
– астон вілла олоф мэллбэрг
3 hours ago




$begingroup$
Thank you for the comment. This is an "exercise" (why this method works) in Ravil Vakil's "A mathematical mosaic". He disguises this principle by keeping the multiplicand and multiplier in base $10$ and not involving binary at all.
$endgroup$
– астон вілла олоф мэллбэрг
3 hours ago












$begingroup$
I just updated the link can you tell me why the author divides by 2 after every iteration
$endgroup$
– MistyD
3 hours ago




$begingroup$
I just updated the link can you tell me why the author divides by 2 after every iteration
$endgroup$
– MistyD
3 hours ago












$begingroup$
It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
$endgroup$
– астон вілла олоф мэллбэрг
2 hours ago




$begingroup$
It is very well linked with what Vakil wrote in his book. Let me edit my answer, explaning how Vakil would do $20 times 13$, and see why the algorithm given in your link reflects both the left shift principle and Vakil's method.
$endgroup$
– астон вілла олоф мэллбэрг
2 hours ago












$begingroup$
Please check the edits. I can add more if required.
$endgroup$
– астон вілла олоф мэллбэрг
2 hours ago




$begingroup$
Please check the edits. I can add more if required.
$endgroup$
– астон вілла олоф мэллбэрг
2 hours ago











3












$begingroup$

I looked at the link, but couldn't find where it says the text you have quoted. Regardless, with what it says & for what you're asking for, i.e., multiplying by using just the shift operator, I believe it means that it works only for multiplying each power of $2$ by left shifting by that power value. You can then use the procedures & code in the link to add the numbers without using arithmetic operators, but with this also using bitwise XOR and AND.



With your example, note that $7 = 2^2 + 2^1 + 2^0$ and $3 = 2^1 + 2^0$. Let's say we start with $7$. Note that $7 times 3$ is $7 times left(2^1 + 2^0right)$. We use the distribution property to get that $7 times 3 = 7 times 2^1 + 7 times 2^0$. For the first term, we can do a left-shift by $1$ bit, the add this to the original value as the second term is already $7$. As such, we get the result to be $left(2^3 + 2^2 + 2^1right) + left(2^2 + 2^1 + 2^0right)$, with this in decimal being $14 + 7 = 21$.



Alternatively, if we started with $3 = 2^1 + 2^0$, we would use that $7 = 2^2 + 2^1 + 2^0$ to do the multiplications & addition to get that $7 times 3 = left(2^3 + 2^2right) + left(2^2 + 2^1right) + left(2^1 + 2^0right) = 21$. In this case, the terms are different, and there are $3$ instead of $2$, but the sum is still the same of course.






share|cite|improve this answer











$endgroup$













  • $begingroup$
    I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
    $endgroup$
    – MistyD
    2 hours ago












  • $begingroup$
    @MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
    $endgroup$
    – John Omielan
    2 hours ago
















3












$begingroup$

I looked at the link, but couldn't find where it says the text you have quoted. Regardless, with what it says & for what you're asking for, i.e., multiplying by using just the shift operator, I believe it means that it works only for multiplying each power of $2$ by left shifting by that power value. You can then use the procedures & code in the link to add the numbers without using arithmetic operators, but with this also using bitwise XOR and AND.



With your example, note that $7 = 2^2 + 2^1 + 2^0$ and $3 = 2^1 + 2^0$. Let's say we start with $7$. Note that $7 times 3$ is $7 times left(2^1 + 2^0right)$. We use the distribution property to get that $7 times 3 = 7 times 2^1 + 7 times 2^0$. For the first term, we can do a left-shift by $1$ bit, the add this to the original value as the second term is already $7$. As such, we get the result to be $left(2^3 + 2^2 + 2^1right) + left(2^2 + 2^1 + 2^0right)$, with this in decimal being $14 + 7 = 21$.



Alternatively, if we started with $3 = 2^1 + 2^0$, we would use that $7 = 2^2 + 2^1 + 2^0$ to do the multiplications & addition to get that $7 times 3 = left(2^3 + 2^2right) + left(2^2 + 2^1right) + left(2^1 + 2^0right) = 21$. In this case, the terms are different, and there are $3$ instead of $2$, but the sum is still the same of course.






share|cite|improve this answer











$endgroup$













  • $begingroup$
    I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
    $endgroup$
    – MistyD
    2 hours ago












  • $begingroup$
    @MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
    $endgroup$
    – John Omielan
    2 hours ago














3












3








3





$begingroup$

I looked at the link, but couldn't find where it says the text you have quoted. Regardless, with what it says & for what you're asking for, i.e., multiplying by using just the shift operator, I believe it means that it works only for multiplying each power of $2$ by left shifting by that power value. You can then use the procedures & code in the link to add the numbers without using arithmetic operators, but with this also using bitwise XOR and AND.



With your example, note that $7 = 2^2 + 2^1 + 2^0$ and $3 = 2^1 + 2^0$. Let's say we start with $7$. Note that $7 times 3$ is $7 times left(2^1 + 2^0right)$. We use the distribution property to get that $7 times 3 = 7 times 2^1 + 7 times 2^0$. For the first term, we can do a left-shift by $1$ bit, the add this to the original value as the second term is already $7$. As such, we get the result to be $left(2^3 + 2^2 + 2^1right) + left(2^2 + 2^1 + 2^0right)$, with this in decimal being $14 + 7 = 21$.



Alternatively, if we started with $3 = 2^1 + 2^0$, we would use that $7 = 2^2 + 2^1 + 2^0$ to do the multiplications & addition to get that $7 times 3 = left(2^3 + 2^2right) + left(2^2 + 2^1right) + left(2^1 + 2^0right) = 21$. In this case, the terms are different, and there are $3$ instead of $2$, but the sum is still the same of course.






share|cite|improve this answer











$endgroup$



I looked at the link, but couldn't find where it says the text you have quoted. Regardless, with what it says & for what you're asking for, i.e., multiplying by using just the shift operator, I believe it means that it works only for multiplying each power of $2$ by left shifting by that power value. You can then use the procedures & code in the link to add the numbers without using arithmetic operators, but with this also using bitwise XOR and AND.



With your example, note that $7 = 2^2 + 2^1 + 2^0$ and $3 = 2^1 + 2^0$. Let's say we start with $7$. Note that $7 times 3$ is $7 times left(2^1 + 2^0right)$. We use the distribution property to get that $7 times 3 = 7 times 2^1 + 7 times 2^0$. For the first term, we can do a left-shift by $1$ bit, the add this to the original value as the second term is already $7$. As such, we get the result to be $left(2^3 + 2^2 + 2^1right) + left(2^2 + 2^1 + 2^0right)$, with this in decimal being $14 + 7 = 21$.



Alternatively, if we started with $3 = 2^1 + 2^0$, we would use that $7 = 2^2 + 2^1 + 2^0$ to do the multiplications & addition to get that $7 times 3 = left(2^3 + 2^2right) + left(2^2 + 2^1right) + left(2^1 + 2^0right) = 21$. In this case, the terms are different, and there are $3$ instead of $2$, but the sum is still the same of course.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited 4 hours ago

























answered 4 hours ago









John OmielanJohn Omielan

3,1001214




3,1001214












  • $begingroup$
    I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
    $endgroup$
    – MistyD
    2 hours ago












  • $begingroup$
    @MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
    $endgroup$
    – John Omielan
    2 hours ago


















  • $begingroup$
    I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
    $endgroup$
    – MistyD
    2 hours ago












  • $begingroup$
    @MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
    $endgroup$
    – John Omielan
    2 hours ago
















$begingroup$
I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
$endgroup$
– MistyD
2 hours ago






$begingroup$
I am sorry John that my initial hyperlink was incorrect. Thank you for pointing out a decent algorithm anyways
$endgroup$
– MistyD
2 hours ago














$begingroup$
@MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
$endgroup$
– John Omielan
2 hours ago




$begingroup$
@MistyD No worries about the hyperlink. Also, you are welcome for my attempt to help you.
$endgroup$
– John Omielan
2 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3122152%2fmultiplying-two-numbers-using-only-the-left-shift-operator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...