InterviewBit - Number of 1 bits  

by ne on 2021-09-29 under Algo/DS/Problems

This problem is a property of InterviewBit (www.interviewbit.com).

Full details of the question, and if you want to try it out yourself, proceed to interviewbit : https://www.interviewbit.com/problems/number-of-1-bits/

Here's the implementation:

 


public class Solution {
    public int numSetBits(long a) {
        int c=64;
        int counter=0;
        while(c!=0 && a!=0){
            if((a ^ (a-1)) ==1){
                counter++;
            }
            a=a>>1;
            c--;
        }
        return counter;
    }
}



Happy Coding !