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