Here you can find the source of bits(int value, int first, int last)
Parameter | Description |
---|---|
value | the value |
first | the first bit |
last | the last bit |
public static int bits(int value, int first, int last)
//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; } }