Java Number Unpack unpackInt(int packedInt, int numBits, int numShiftedLeft)

Here you can find the source of unpackInt(int packedInt, int numBits, int numShiftedLeft)

Description

unpack Int

License

Open Source License

Parameter

Parameter Description
packedInt a parameter
numBits a parameter
numShiftedLeft a parameter

Declaration

public static int unpackInt(int packedInt, int numBits,
        int numShiftedLeft) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2013 SKRATCHDOT.COM
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *  // ww  w . j  av  a2 s . co  m
 * Contributors:
 *     JEFF |:at:| SKRATCHDOT |:dot:| COM
 *******************************************************************************/

public class Main {
    /**
     * @param packedInt
     * @param numBits
     * @param numShiftedLeft
     * @return
     */
    public static int unpackInt(int packedInt, int numBits,
            int numShiftedLeft) {
        //   The comments below assume we are passing in:
        //      packedInt      = y010 yyyy
        //      numBits         = 3
        //      numShiftedLeft   = 4
        //   We want to end up with: 0000 0010

        int bitsUnshifted = (int) (Math.pow(2, numBits) - 1); // 0000 0111
        int bitsShifted = bitsUnshifted << numShiftedLeft; // 0111 0000
        int outputValue = packedInt & bitsShifted; // 0yyy 0000
        return outputValue >> numShiftedLeft; // 0000 0yyy
    }
}

Related

  1. unpack64(long num)
  2. unpackDigital(int packed)
  3. unpackInt(final int argb, final int type)
  4. unpackInt(long theLong, boolean isFirst)
  5. unpackInts(long... longs)
  6. unpackKmer(final long value)
  7. unpackLong(long a, int bits)