Java Bits Convert to bitRangeValueLong(byte[] b, int offset, int length)

Here you can find the source of bitRangeValueLong(byte[] b, int offset, int length)

Description

Returns the value of the bits in the given range.

License

Open Source License

Parameter

Parameter Description
b the array of bytes.
offset the location at which to begin
length the number of bits to include in the value.

Return

the value of the bits in the range.

Declaration

public static long bitRangeValueLong(byte[] b, int offset, int length) 

Method Source Code

//package com.java2s;
/*/*from www .j  a  v  a 2s .c  o  m*/
 * ============================================================================
 * GNU General Public License
 * ============================================================================
 *
 * Copyright (C) 2015 Infinite Automation Software. All rights reserved.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * When signing a commercial license with Infinite Automation Software,
 * the following extension to GPL is made. A special exception to the GPL is
 * included to allow you to distribute a combined work that includes BAcnet4J
 * without being obliged to provide the source code for any proprietary components.
 *
 * See www.infiniteautomation.com for commercial license options.
 * 
 * @author Matthew Lohbihler
 */

public class Main {
    private static int[] bitFromMask = { 0xff, 0x7f, 0x3f, 0x1f, 0xf, 0x7,
            0x3, 0x1 };

    /**
     * Returns the value of the bits in the given range. Ranges can extend multiple bytes. No range checking is done.
     * Invalid ranges will result in {@link ArrayIndexOutOfBoundsException}.
     * 
     * @param b
     *            the array of bytes.
     * @param offset
     *            the location at which to begin
     * @param length
     *            the number of bits to include in the value.
     * @return the value of the bits in the range.
     */
    public static long bitRangeValueLong(byte[] b, int offset, int length) {
        if (length <= 0)
            return 0;

        int byteFrom = offset / 8;
        int byteTo = (offset + length - 1) / 8;

        long result = b[byteFrom] & bitFromMask[offset % 8];

        for (int i = byteFrom + 1; i <= byteTo; i++) {
            result <<= 8;
            result |= b[i] & 0xff;
        }

        result >>= 8 - (((offset + length - 1) % 8) + 1);

        return result;
    }
}

Related

  1. bitFlagByteLength(final int nbits)
  2. bitHistogram(int[] data)
  3. bitMap(final boolean v1, final boolean v2, final boolean v3, final boolean v4, final boolean v5, final boolean v6, final boolean v7, final boolean v8)
  4. bitmap64(byte... table)
  5. BitPrecisionInt(int numberValue)
  6. bitReverse31(int i)
  7. bitrv208(double[] a, int offa)
  8. bitrv208neg(double[] a, int offa)
  9. bitrv216neg(double[] a, int offa)