알고리즘 96- Bit Counting

jabae·2021년 11월 4일
0

알고리즘

목록 보기
96/97

Q.

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case

A)

var countBits = function(n) {
  return n.toString(2).split('').filter(el => el === '1').length
};
profile
it's me!:)

0개의 댓글