forked from trekhleb/javascript-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateBit.js
More file actions
Latest commit
16 lines (14 loc) · 473 Bytes
/
Copy pathupdateBit.js
File metadata and controls
16 lines (14 loc) · 473 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @param {number} number
* @param {number} bitPosition - zero based.
* @param {number} bitValue - 0 or 1.
* @return {number}
*/
exportdefaultfunctionupdateBit(number,bitPosition,bitValue){
// Normalized bit value.
constbitValueNormalized=bitValue ? 1 : 0;
// Init clear mask.
constclearMask=~(1<<bitPosition);
// Clear bit value and then set it up to required value.
return(number&clearMask)|(bitValueNormalized<<bitPosition);
}