Monday, December 3, 2018

java - counting number of ones in binary representation of a number








I want to find out how many 1s are there in binary representation of a number.I have 2 logic .




  1.   int count =0;
    int no = 4;

    while(no!=0){

    int d = no%2;
    if(d==1)
    count++;
    no = no/2;
    str = str+ d;
    }

  2. Now second logic is to keep on masking number iteratively with 1,2,4,8,32 and check if result is 1,2,4, 8..... Am not geting what should be ending condition for this loop.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...