count Number Bits - Java java.lang

Java examples for java.lang:Math Number

Description

count Number Bits

Demo Code


//package com.java2s;

public class Main {
    public static int countNumBits(int N) {
        int v = N; // count the number of bits set in v
        int c; // c accumulates the total bits set in v

        for (c = 0; v != 0; c++) {
            v &= v - 1; // clear the least significant bit set
        }/*from   w w w .  j  a v  a2  s  . co m*/

        return c;
    }
}

Related Tutorials