[challenge]Find number of set bits in a number with in an iteration? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[challenge]Find number of set bits in a number with in an iteration?

must find in one iteration

1st Feb 2018, 7:39 AM
Amal Poulose
Amal Poulose - avatar
11 Answers
+ 2
Here is the oneliner fast solution https://code.sololearn.com/ckyITt17dCeJ/?ref=app
1st Feb 2018, 4:33 PM
VcC
VcC - avatar
+ 4
Do you define "set bits" as bits = 1, so that 7 --> 0111 ie 3 set bits. if so in python num=7 print(len(''.join(str(bin(num))[2:].split('0'))))
1st Feb 2018, 8:08 AM
Louis
Louis - avatar
1st Feb 2018, 12:13 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
create a table then lookup in the table : gives you the bit set in one operation
1st Feb 2018, 10:45 AM
VcC
VcC - avatar
+ 2
you can do it, but I'm not familiar with java
1st Feb 2018, 11:37 AM
Amal Poulose
Amal Poulose - avatar
1st Feb 2018, 11:51 AM
Jeremy
Jeremy - avatar
+ 2
@Amal Poulose Corrected the code and now it works fine.
1st Feb 2018, 5:03 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
you have to find it in single iteration means no loops
1st Feb 2018, 9:24 AM
Amal Poulose
Amal Poulose - avatar
+ 1
is java allowed?
1st Feb 2018, 11:33 AM
Jeremy
Jeremy - avatar
+ 1
@kinshuk vasisht check your code with upper limit of long long int
1st Feb 2018, 4:41 PM
Amal Poulose
Amal Poulose - avatar
3rd Feb 2018, 5:54 AM
Amal Poulose
Amal Poulose - avatar