-
Solution to the Bits TopCoder challenge...
Hi Java Coders,
For the fun of it, I've made a simple Java solution to the Bits TopCoder Challenge.
The solution simply counts the number of times you can strip off a bit from the right until you reach zero.
The signless bit-shift operator '>>>' is used to make sure that 0 bits are shifted in from the left.
Is there a simpler solution to this challenge without iteration?
public class Bits
{
public int minBits(int n) {
int bitCount= 0;
while (n != 0) {
bitCount++;
n= n >>> 1;
}
return bitCount;
}
}
Kind regards,
Kim Dam Petersen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks