Java Set Cardinality cardinality(byte b)

Here you can find the source of cardinality(byte b)

Description

cardinality

License

Apache License

Declaration

public static int cardinality(byte b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Martijn Courteaux <martijn.courteaux@skynet.be>
 * //from   w  w w. j a v a 2s.c  o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    public static int cardinality(byte b) {
        int count = 0;
        for (int i = 0; i < 8; ++i) {
            if (((b >>> i) & 1) == 1) {
                ++count;
            }
        }
        return count;
    }

    public static int cardinality(int i, int max) {
        int count = 0;
        for (int j = 0; j < max; ++j) {
            if (((i >>> j) & 1) == 1) {
                ++count;
            }
        }
        return count;
    }
}

Related

  1. cardinality(final int from, final int to, final long[] words)
  2. cardinality(long v)
  3. cardinalityInBitmapRange(long[] bitmap, int start, int end)
  4. cardinalityInBitmapWordRange(long[] bitmap, int start, int end)