Avatar 🍥

Coinidea's Blog

Life is random

  1. Home
  2. Archives
  3. Search
  4. Resume
  5. Links
    1. Dark Mode
Beauty of Programming

[Beauty of Programming 2.1] Count the Number of 1s in Binary

May 23, 2015
1 minute read
中文
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int CountOne(int val)
{
    int cnt = 0;
    while (val)
    {
        val &= val - 1;
        cnt++;
    }
    return cnt;
}
Beauty of Programming
Licensed under CC BY-NC-SA 4.0

Related content

[Beauty of Programming 2.9] Fibonacci Sequence

[Beauty of Programming 2.8] Finding Integers That Meet the Criteria

[Beauty of Programming 2.7] The Greatest Common Divisor Problem

[Beauty of Programming 2.6] Exact Representation of Floating Point Numbers

[Beauty of Programming 2.5] Finding the K Largest Numbers

© 2020 - 2026 Coinidea's Blog
京ICP备15014655号