MCPcopy Create free account
hub / github.com/apna-college/Alpha / countSetBits2

Method countSetBits2

5_Bit Manipulation/BitOperations.java:68–76  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

66
67 //a faster method to count set bits
68 public static int countSetBits2(int n) {
69 int setBits = 0;
70 while(n>0) {
71 // removes the last set bit from curr number
72 n = n & (n-1);
73 setBits++;
74 }
75 return setBits;
76 }
77 public static void main(String args[]) {
78 System.out.println(countSetBits(9));
79 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected