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

Commit792f490

Browse files
committed
Add bit manipulation section.
1 parent36e0bfe commit792f490

File tree

10 files changed

+152
-0
lines changed

10 files changed

+152
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +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
5253
*`B`[Factorial](src/algorithms/math/factorial)
5354
*`B`[Fibonacci Number](src/algorithms/math/fibonacci)
5455
*`B`[Primality Test](src/algorithms/math/primality-test) (trial division method)

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#Bit Manipulation
2+
3+
####Get Bit
4+
5+
This method shifts`1` over by`bitPosition` bits, creating a
6+
value that looks like`00100`. Then we perform`AND` operation
7+
that clears all bits from the original number except the
8+
`bitPosition` one. Then we compare the result with zero. If
9+
result is zero that would mean that original number has`0` at
10+
position`bitPosition`.
11+
12+
>See`getBit` function for further details.
13+
14+
####Set Bit
15+
16+
This method shifts`1` over by`bitPosition` bits, creating a
17+
value that looks like`00100`. Then we perform`OR` operation
18+
that sets specific bit into`1` but it does not affect on
19+
other bits of the number.
20+
21+
>See`setBit` function for further details.
22+
23+
####Clear Bit
24+
25+
This method shifts`1` over by`bitPosition` bits, creating a
26+
value that looks like`00100`. Than it inverts this mask to get
27+
the number that looks like`11011`. Then`AND` operation is
28+
being applied to both the number and the mask. That operation
29+
unsets the bit.
30+
31+
>See`clearBit` function for further details.
32+
33+
####Update Bit
34+
35+
This method is a combination of "Clear Bit" and "Set Bit" methods.
36+
37+
>See`updateBit` function for further details.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
importclearBitfrom'../clearBit';
2+
3+
describe('clearBit',()=>{
4+
it('should clear bit at specific position',()=>{
5+
// 1 = 0b0001
6+
expect(clearBit(1,0)).toBe(0);
7+
expect(clearBit(1,1)).toBe(1);
8+
expect(clearBit(1,2)).toBe(1);
9+
10+
// 10 = 0b1010
11+
expect(clearBit(10,0)).toBe(10);
12+
expect(clearBit(10,1)).toBe(8);
13+
expect(clearBit(10,3)).toBe(2);
14+
});
15+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
importgetBitfrom'../getBit';
2+
3+
describe('getBit',()=>{
4+
it('should get bit at specific position',()=>{
5+
// 1 = 0b0001
6+
expect(getBit(1,0)).toBe(1);
7+
expect(getBit(1,1)).toBe(0);
8+
9+
// 2 = 0b0010
10+
expect(getBit(2,0)).toBe(0);
11+
expect(getBit(2,1)).toBe(1);
12+
13+
// 3 = 0b0011
14+
expect(getBit(3,0)).toBe(1);
15+
expect(getBit(3,1)).toBe(1);
16+
17+
// 10 = 0b1010
18+
expect(getBit(10,0)).toBe(0);
19+
expect(getBit(10,1)).toBe(1);
20+
expect(getBit(10,2)).toBe(0);
21+
expect(getBit(10,3)).toBe(1);
22+
});
23+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
importsetBitfrom'../setBit';
2+
3+
describe('setBit',()=>{
4+
it('should set bit at specific position',()=>{
5+
// 1 = 0b0001
6+
expect(setBit(1,0)).toBe(1);
7+
expect(setBit(1,1)).toBe(3);
8+
expect(setBit(1,2)).toBe(5);
9+
10+
// 10 = 0b1010
11+
expect(setBit(10,0)).toBe(11);
12+
expect(setBit(10,1)).toBe(10);
13+
expect(setBit(10,2)).toBe(14);
14+
});
15+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
importupdateBitfrom'../updateBit';
2+
3+
describe('updateBit',()=>{
4+
it('should update bit at specific position',()=>{
5+
// 1 = 0b0001
6+
expect(updateBit(1,0,1)).toBe(1);
7+
expect(updateBit(1,0,0)).toBe(0);
8+
expect(updateBit(1,1,1)).toBe(3);
9+
expect(updateBit(1,2,1)).toBe(5);
10+
11+
// 10 = 0b1010
12+
expect(updateBit(10,0,1)).toBe(11);
13+
expect(updateBit(10,0,0)).toBe(10);
14+
expect(updateBit(10,1,1)).toBe(10);
15+
expect(updateBit(10,1,0)).toBe(8);
16+
expect(updateBit(10,2,1)).toBe(14);
17+
expect(updateBit(10,2,0)).toBe(10);
18+
});
19+
});

‎src/algorithms/math/bits/clearBit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
*@param {number} number
3+
*@param {number} bitPosition - zero based.
4+
*@return {number}
5+
*/
6+
exportdefaultfunctionclearBit(number,bitPosition){
7+
constmask=~(1<<bitPosition);
8+
9+
returnnumber&mask;
10+
}

‎src/algorithms/math/bits/getBit.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*@param {number} number
3+
*@param {number} bitPosition - zero based.
4+
*@return {number}
5+
*/
6+
exportdefaultfunctiongetBit(number,bitPosition){
7+
return(number&(1<<bitPosition))===0 ?0 :1;
8+
}

‎src/algorithms/math/bits/setBit.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*@param {number} number
3+
*@param {number} bitPosition - zero based.
4+
*@return {number}
5+
*/
6+
exportdefaultfunctionsetBit(number,bitPosition){
7+
returnnumber|(1<<bitPosition);
8+
}

‎src/algorithms/math/bits/updateBit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*@param {number} number
3+
*@param {number} bitPosition - zero based.
4+
*@param {number} bitValue - 0 or 1.
5+
*@return {number}
6+
*/
7+
exportdefaultfunctionupdateBit(number,bitPosition,bitValue){
8+
// Normalized bit value.
9+
constbitValueNormalized=bitValue ?1 :0;
10+
11+
// Init clear mask.
12+
constclearMask=~(1<<bitPosition);
13+
14+
// Cleat bit value and then set it up to required value.
15+
return(number&clearMask)|(bitValueNormalized<<bitPosition);
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp