Count set bits in int
Java
Medium
6 views
Problem Description
Task: return number of set bits (1s) in binary representation of x.
Output Format
Return value
Constraints
Handle negative using unsigned shift.
Official Solution
static int bitCount(int x){int c=0;while(x!=0){c+=x&1;x>>>=1;}return c;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!