Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc268203

Browse files
committed
Add more bit manipulation functions.
1 parent792f490 commitc268203

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ a set of rules that precisely define a sequence of operations.
4949
###Algorithms by Topic
5050

5151
***Math**
52-
*`B`[Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits
52+
*`B`[Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two etc.
5353
*`B`[Factorial](src/algorithms/math/factorial)
5454
*`B`[Fibonacci Number](src/algorithms/math/fibonacci)
5555
*`B`[Primality Test](src/algorithms/math/primality-test) (trial division method)

‎src/algorithms/math/bits/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,37 @@ unsets the bit.
3535
This method is a combination of "Clear Bit" and "Set Bit" methods.
3636

3737
>See`updateBit` function for further details.
38+
39+
####Multiply By Two
40+
41+
This method shifts original number by one bit to the left.
42+
Thus all binary number components (powers of two) are being
43+
multiplying by two and thus the number itself is being
44+
multiplied by two.
45+
46+
```
47+
Before the shift
48+
Number: 0b0101 = 5
49+
Powers of two: 0 + 2^2 + 0 + 2^0
50+
51+
After the shift
52+
Number: 0b1010 = 10
53+
Powers of two: 2^3 + 0 + 2^1 + 0
54+
```
55+
56+
####Divide By Two
57+
58+
This method shifts original number by one bit to the right.
59+
Thus all binary number components (powers of two) are being
60+
divided by two and thus the number itself is being
61+
divided by two without remainder.
62+
63+
```
64+
Before the shift
65+
Number: 0b0101 = 5
66+
Powers of two: 0 + 2^2 + 0 + 2^0
67+
68+
After the shift
69+
Number: 0b0010 = 2
70+
Powers of two: 0 + 0 + 2^1 + 0
71+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
importdivideByTwofrom'../divideByTwo';
2+
3+
describe('divideByTwo',()=>{
4+
it('should divide numbers by two using bitwise operations',()=>{
5+
expect(divideByTwo(0)).toBe(0);
6+
expect(divideByTwo(1)).toBe(0);
7+
expect(divideByTwo(3)).toBe(1);
8+
expect(divideByTwo(10)).toBe(5);
9+
expect(divideByTwo(17)).toBe(8);
10+
expect(divideByTwo(125)).toBe(62);
11+
});
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
importmultiplyByTwofrom'../multiplyByTwo';
2+
3+
describe('multiplyByTwo',()=>{
4+
it('should multiply numbers by two using bitwise operations',()=>{
5+
expect(multiplyByTwo(0)).toBe(0);
6+
expect(multiplyByTwo(1)).toBe(2);
7+
expect(multiplyByTwo(3)).toBe(6);
8+
expect(multiplyByTwo(10)).toBe(20);
9+
expect(multiplyByTwo(17)).toBe(34);
10+
expect(multiplyByTwo(125)).toBe(250);
11+
});
12+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
*@param {number} number
3+
*@return {number}
4+
*/
5+
exportdefaultfunctiondivideByTwo(number){
6+
returnnumber>>1;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
*@param {number} number
3+
*@return {number}
4+
*/
5+
exportdefaultfunctionmultiplyByTwo(number){
6+
returnnumber<<1;
7+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp