Java Number Unpack unpackKmer(final long value)

Here you can find the source of unpackKmer(final long value)

Description

unpack Kmer

License

Open Source License

Declaration

public static byte[] unpackKmer(final long value) 

Method Source Code

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

public class Main {
    private static int BITS_PER_BASE = 4;
    private static int SINGLE_BASE_MASK = 0xf;

    public static byte[] unpackKmer(final long value) {
        final int highest = (int) Long.numberOfTrailingZeros(Long
                .highestOneBit(value));/*  w  w  w.ja va2  s .  c  o  m*/
        final int n = highest / BITS_PER_BASE
                + ((highest % BITS_PER_BASE) > 0 ? 1 : 0);
        byte[] result = new byte[n];
        // Fill right-to-left
        for (int i = 0; i < n; i++) {
            // This nastiness just extracts a specific base (4 bits) by
            // masking and shifting.
            final int bitOffset = i * BITS_PER_BASE;
            result[n - 1 - i] = (byte) ((value & (SINGLE_BASE_MASK << bitOffset)) >> bitOffset);
        }
        return result;
    }
}

Related

  1. unpackDigital(int packed)
  2. unpackInt(final int argb, final int type)
  3. unpackInt(int packedInt, int numBits, int numShiftedLeft)
  4. unpackInt(long theLong, boolean isFirst)
  5. unpackInts(long... longs)
  6. unpackLong(long a, int bits)