Count bits using Kernighan
Java
Medium
6 views
Problem Description
Task: count set bits using x &= (x-1) loop.
Output Format
Return value
Constraints
Use unsigned shift not required here.
Official Solution
static int bitCountFast(int x){int c=0;while(x!=0){x&=(x-1);c++;}return c;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!