Monday 3 December 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...