Given a positive integer N, print count of set bits in it. 
Example 1:
Input:
N = 6
Output:
2
Explanation:
Binary representation is '110' 
So the count of the set bit is 2.
Example 2:
Input:
8
Output:
1
Explanation:
Binary representation is '1000' 
So the count of the set bit is 1.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function setBits() which takes an Integer N and returns the count of number of set bits.
Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3
class Solution:
	def setBits(self, N):
		# code here

Solutions:
class Solution:

	def setBits(self, N):
		dnum = N
		i = 0
		bnum = []
		while dnum != 0:
			rem = dnum % 2
			bnum.insert(i, rem)
			i = i + 1
			dnum = int(dnum / 2)
		x = 0
		for f in bnum:
			if f == 1:
				x = x + 1
		return x


class Solution:

	def setBits(self, N):
		c = 0
		while N:
			c += 1
			N = N & N - 1
		return c


class Solution:

	def setBits(self, n):
		count = 0
		while n > 0:
			if n % 2 == 1:
				count += 1
			n //= 2
		return count


class Solution:

	def setBits(self, N):
		if N <= 1:
			return 1
		a = 0
		while N > 1:
			x = N % 2
			N = N // 2
			if x == 1:
				a += 1
		return a + 1


class Solution:

	def setBits(self, N):
		n = bin(N)
		n = n[2:]
		return n.count('1')


class Solution:

	def setBits(self, N):
		return bin(N).count('1')


class Solution:

	def setBits(self, N):
		count = 0
		while N != 0:
			if N & 1 == 1:
				count += 1
			N >>= 1
		return count


class Solution:

	def setBits(self, N):
		n = bin(N).replace('0b', '')
		count = 0
		for i in n:
			if i == '1':
				count = count + 1
		return count


class Solution:

	def setBits(self, N):
		count = 0
		n = N
		while n > 0:
			n = n & n - 1
			count = count + 1
		return count


class Solution:

	def setBits(self, N):
		res = 0
		while N > 0:
			res += 1
			N = N & N - 1
		return res


class Solution:

	def setBits(self, N):
		x = bin(int(N))
		y = x[2:]
		lis = list(y)
		return lis.count('1')


class Solution:

	def setBits(self, N):
		A = bin(N)[2:]
		ans = 0
		for i in A:
			if i == '1':
				ans += 1
		return ans


class Solution:

	def setBits(self, N):
		n = bin(N)
		return str(n).count('1')


class Solution:

	def setBits(self, N):
		s = bin(N)
		d = s[2:]
		k = d.count('1')
		return k


class Solution:

	def setBits(self, N):
		d = bin(N)
		c = d.count('1')
		return c


class Solution:

	def setBits(self, N):
		cnt = 0
		for i in bin(N):
			if i == '1':
				cnt += 1
		return cnt


class Solution:

	def setBits(self, N):
		c = 0
		while N != 0:
			bit = N & 1
			if bit:
				c += 1
			N = N >> 1
		return c


class Solution:

	def setBits(self, N):
		t = bin(N)[2:]
		p = t.count('1')
		return p


class Solution:

	def setBits(self, N):
		b = bin(N)
		a = b.count('1')
		return a


class Solution:

	def setBits(self, N):
		binary = bin(N)[2:]
		return binary.count('1')


class Solution:

	def setBits(self, N):
		x = bin(N)
		z = str(x).count('1')
		return z


class Solution:

	def setBits(self, N):
		b = bin(N)
		b = b[2:]
		count = 0
		l = list(str(b))
		for i in l:
			if i == '1':
				count = count + 1
		return count


class Solution:

	def setBits(self, N):
		count = 0
		binary = str(bin(N))
		for i in binary:
			if i == str(1):
				count += 1
		return count


class Solution:

	def setBits(self, N):
		c = 0
		x = bin(N)
		for i in x:
			if i == '1':
				c += 1
		return c


class Solution:

	def setBits(self, N):
		x = bin(N)
		m = x.count('1')
		return m


class Solution:

	def setBits(self, N):
		ob = bin(N)
		b = ob[2:]
		s = str(b)
		return s.count('1')


class Solution:

	def setBits(self, N):
		a = bin(N)
		m = str(a).count('1')
		return m


class Solution:

	def setBits(self, n):
		res = 0
		while n:
			n &= n - 1
			res += 1
		return res


class Solution:

	def setBits(self, N):
		dectobinary = bin(N)[2:]
		return dectobinary.count('1')


class Solution:

	def setBits(self, N):
		b = str(format(N, 'b'))
		res = 0
		for i in range(len(b)):
			if b[i] == '1':
				res += 1
		return res


class Solution:

	def setBits(self, N):
		binary_n = str(bin(int(N)))[2:]
		cnt = 0
		for i in binary_n:
			if int(i) == 1:
				cnt += 1
		return cnt


class Solution:

	def setBits(self, N):
		cnt = 0
		for i in range(32):
			if N & 1 << i:
				cnt += 1
		return cnt


class Solution:

	def setBits(self, N):
		no_of_digits = 0
		while N:
			if N % 2:
				no_of_digits += 1
			N = N >> 1
		return no_of_digits


class Solution:

	def setBits(self, N):
		b = bin(N)[2:]
		return b.count('1')


class Solution:

	def setBits(self, N):
		cnt = 0
		while N > 0:
			if N & 1 == 1:
				cnt = cnt + 1
			N = N >> 1
		return cnt


class Solution:

	def setBits(self, N):
		b = '{:b}'.format(N)
		return b.count('1')


class Solution:

	def setBits(self, N):
		b = bin(N)[2:]
		c = 0
		for i in b:
			if i == '1':
				c += 1
		return c


class Solution:

	def setBits(self, N):
		N = str(bin(N))
		N = N[2:]
		return N.count('1')


class Solution:

	def setBits(self, n):
		l = []
		while n > 0:
			l.append(n % 2)
			n //= 2
		k = l.count(1)
		return k


import math

class Solution:

	def setBits(self, N):
		n = int(math.log2(N)) + 1
		count = 0
		for i in range(n):
			if 1 << i & N:
				count += 1
		return count


import math

class Solution:

	def setBits(self, N):
		count = 0
		S = str(bin(N))
		for i in S:
			if i == '1':
				count += 1
		return count


class Solution:

	def setBits(self, N):
		a = list(bin(N))
		count = 0
		for i in range(len(a)):
			if str(1) == a[i]:
				count += 1
		return count


class Solution:

	def setBits(self, N):
		s = bin(N)
		c = 0
		for i in range(2, len(s)):
			if s[i] == '1':
				c += 1
		return c


import math

class Solution:

	def setBits(self, N):
		no_bits = math.ceil(math.log2(N))
		set_bits = 0
		for i in range(no_bits + 1):
			if N & 1 << i:
				set_bits += 1
		return set_bits


class Solution:

	def setBits(self, N):
		(k, ans) = (N, 0)
		while k > 0:
			if k & 1 == 1:
				ans += 1
			k = k // 2
		return ans


class Solution:

	def setBits(self, N):
		ans = 0
		while N:
			N = N & N - 1
			ans += 1
		return ans


class Solution:

	def setBits(self, N):
		countSetBit = 0
		while N != 0:
			lastBit = N & 1
			if lastBit == 1:
				countSetBit += 1
			N = N >> 1
		return countSetBit


class Solution:

	def setBits(self, n):
		s = bin(n).replace('0b', ' ')
		s = str(s)
		c = 0
		for i in s:
			if i == '1':
				c += 1
		return c