Java Bit Count countBits(byte num)

Here you can find the source of countBits(byte num)

Description

count Bits

License

Apache License

Declaration

public static int countBits(byte num) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static int countBits(byte num) {
        int count = 0;
        for (int i = 0; i < 8; i++) {
            if ((num & 1) == 1) // check if right most bit is 1
            {//from  ww w  . j  a  v  a  2 s.co  m
                count++;
            }
            num = (byte) (num >>> 1); // shit right 1 bit, including the sign
            // bit
        }
        return count;
    }
}

Related

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