Java Bit Count countBits(int mask)

Here you can find the source of countBits(int mask)

Description

count Bits

License

Open Source License

Declaration

private static int countBits(int mask) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  ww  . ja  va  2 s . c  om
 *    IBM Corporation - initial API and implementation 
 ****************************************************************************/

public class Main {
    private static int countBits(int mask) {
        int count = 0;
        for (int index = 0; index < 32; index++) {
            if ((mask & 0x1) != 0) {
                count++;
            }
            mask = mask >>> 1;
        }

        return count;
    }
}

Related

  1. bitSizeForUnsignedValue(final int value)
  2. countBits (long v)
  3. countBits(byte num)
  4. countBits(final int intValue)
  5. countBits(int i)
  6. countBits(int n)
  7. countBits(int x)
  8. countBits(int x)
  9. countBits(long num)