find number of 1 bits(hamming weight)

PHOTO EMBED

Fri Apr 05 2024 07:22:23 GMT+0000 (Coordinated Universal Time)

Saved by @hiimsa #java

 // find number of 1 bits
        int n = 10; //101
        int count =0;
        // will repeatedly do and of number with(n-1), as this flips the LSB 1 bit
        while(n !=0) {
            count++;
            n &= (n-1);

        }
        System.out.println("count is " + count);
content_copyCOPY