From 7f189498cff2be4bc89e16f93c9dd4a4d6066eee Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 10 Aug 2018 18:19:58 +0300 Subject: [PATCH] Update BitWise Operation README. --- src/algorithms/math/bits/README.md | 28 ++++++++++++-------- src/algorithms/math/bits/multiplyUnsigned.js | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md index d921f8ea..0498e270 100644 --- a/src/algorithms/math/bits/README.md +++ b/src/algorithms/math/bits/README.md @@ -91,24 +91,30 @@ inverting all of the bits of the number and adding 1 to it. > See `switchSign` function for further details. -#### Multiply +#### Multiply Two Numbers -This method multiplies two numbers(integer) using bitwise operators. This method is based on that "Every number can be denoted as the sum of powers of 2". +This method multiplies two integer numbers using bitwise operators. +This method is based on that "Every number can be denoted as the sum of powers of 2". -``` -Let us take two numbers number1 and number2. +The main idea of bitwise multiplication is that every number may be split +to the sum of powers of two: -number1 * number2 = number1 * (Representation in Base 2) +I.e. -Let us take number2 = 8 = 0b 1000 - -number1 * number2 = number1 * (1*8 + 0*4 + 0*2 + 0*1) - = number1 * 1 * (1<<3) + number1 * 0 * (1<<2) + number1 * 0 * (1<<1) + number1 * 0 * (1<<0) - = (number1<<3) * 1 + (number1<<2) * 0 + (number1<<1) * 0 + (number1<<0) * 0 +```text +19 = 2^4 + 2^1 + 2^0 ``` +Then multiplying number `x` by `19` is equivalent of: -> See `multiply` function for further details. +```text +x * 19 = x * 2^4 + x * 2^1 + x * 2^0 +``` + +Now we need to remember that `x * 2^4` is equivalent of shifting `x` left +by `4` bits ()`x << 4`). + +> See `multiplyUnsigned` function for further details. ## References diff --git a/src/algorithms/math/bits/multiplyUnsigned.js b/src/algorithms/math/bits/multiplyUnsigned.js index e129eec8..17a2c95a 100644 --- a/src/algorithms/math/bits/multiplyUnsigned.js +++ b/src/algorithms/math/bits/multiplyUnsigned.js @@ -2,7 +2,7 @@ * Multiply to unsigned numbers using bitwise operator. * * The main idea of bitwise multiplication is that every number may be split - * to the sum of posers of two: + * to the sum of powers of two: * * I.e. 19 = 2^4 + 2^1 + 2^0 *