Java Array Normalize norm(byte[] tab)

Here you can find the source of norm(byte[] tab)

Description

Computes the number of 1's in the byte array

License

Open Source License

Parameter

Parameter Description
tab a parameter

Declaration

public static short norm(byte[] tab) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w  w w .j ava2 s  .  co m
     * Computes the number of 1's in the byte array
     * @param tab
     * @return
     */
    public static short norm(byte[] tab) {
        short count = 0;
        for (byte b : tab) {
            count += bitCount(b);
        }
        return count;
    }

    /**
     * Hamming distance of a byte with 0
     * @param b the byte on which we compute the hamming distance with 0
     * @return the number of 1's in the binary representation of the byte
     */
    public static short bitCount(byte b) {
        short temp = (short) (b + 128);
        short count = 0;
        for (int i = 0; i < 8; i++) {
            count += (temp >> (7 - i)) % 2;
        }
        return count;
    }
}

Related

  1. norm(double[] a)
  2. norm(double[] a)
  3. norm(double[] a)
  4. norm(double[] array)