Skip to content

Commit 413cfa4

Browse files
author
Ujjwal Aryal
authored
Create extractBits.js
Function to extract 'k' bits from position 'p' and returns the extracted value as integer.
1 parent 7a4b829 commit 413cfa4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} number, size(k), position(p)
3+
* @return {number}
4+
*/
5+
6+
7+
/* Function to extract 'k' bits from position 'p'
8+
* and returns the extracted value.
9+
*
10+
*/
11+
export default function extractBits(number, k, p) {
12+
13+
return (((1 << k) - 1) & (number >> (p - 1)));
14+
15+
}

0 commit comments

Comments
 (0)