-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflipping-bits.ts
More file actions
26 lines (20 loc) · 635 Bytes
/
Copy pathflipping-bits.ts
File metadata and controls
26 lines (20 loc) · 635 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]
*/
const __BINARY_BASE__ = 2;
const __NUMBER_SIZE_IN_BITS__ = 32;
function flippingBits(n: number): number {
let nBinaryStr = n.toString(__BINARY_BASE__);
nBinaryStr = nBinaryStr.padStart(__NUMBER_SIZE_IN_BITS__, '0');
let resultBinStr = '';
nBinaryStr.split('').forEach((binDigit) => {
if (binDigit === '1') {
resultBinStr += '0';
} else {
resultBinStr += '1';
}
});
return Number.parseInt(resultBinStr, __BINARY_BASE__);
}
export default { flippingBits };
export { flippingBits };