mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 19:17:33 +08:00
Merge pull request #316 from vivek9patel/master
added bit_manipulation folder & binary_count_setbits.js
This commit is contained in:
17
Bit-Manipulation/BinaryCountSetBits.js
Normal file
17
Bit-Manipulation/BinaryCountSetBits.js
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
author: vivek9patel
|
||||
license: GPL-3.0 or later
|
||||
|
||||
This script will find number of 1's
|
||||
in binary representain of given number
|
||||
|
||||
*/
|
||||
|
||||
function BinaryCountSetBits (a) {
|
||||
'use strict'
|
||||
// convert number into binary representation and return number of set bits in binary representaion
|
||||
return a.toString(2).split('1').length - 1
|
||||
}
|
||||
|
||||
// Run `binary_and` Function to find the binary and operation
|
||||
console.log(BinaryCountSetBits(251))
|
Reference in New Issue
Block a user