Java Bit Get bits(int value, int first, int last)

Here you can find the source of bits(int value, int first, int last)

Description

Get the specified bits from the specified value.

License

Open Source License

Parameter

Parameter Description
value the value
first the first bit
last the last bit

Return

the specified bits from the specified value

Declaration

public static int bits(int value, int first, int last) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2012 by Min Cai (min.cai.china@gmail.com).
 *
 * This file is part of the PickaPack library.
 *
 * PickaPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.//  w w  w  . j  a v a2 s  .  co m
 *
 * PickaPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PickaPack. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    /**
     * Get the specified bits from the specified value.
     *
     * @param value the value
     * @param first the first bit
     * @param last the last bit
     * @return the specified bits from the specified value
     */
    public static int bits(int value, int first, int last) {
        return (value >> last) & mask(first - last + 1);
    }

    /**
     * Create the mask of the specified length.
     *
     * @param numBits the length of the mask
     * @return the mask
     */
    public static int mask(int numBits) {
        return (1 << numBits) - 1;
    }
}

Related

  1. bitAt(int offset, byte aByte)
  2. bits(int i)
  3. bits(int i)
  4. bits(int n, int offset, int length)
  5. bits(int value, int c)
  6. bits(long maxValue)
  7. bits(long value, int sb, int eb)