Here you can find the source of isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
Parameter | Description |
---|---|
bytes | a parameter |
byteOrder | a parameter |
isSigned | a parameter |
private static boolean isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
//package com.java2s; //License from project: Apache License import java.nio.ByteOrder; public class Main { public final static int BITS_PER_BYTE = 8; /**// w w w . jav a 2 s.c om * Detect whether this a negative value. * * @param bytes * @param byteOrder * @param isSigned * @return true if the provided details indicate a negative valued number */ private static boolean isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned) { boolean isNegative = false; if (isSigned) { byte mostSignificantByte = getMostSignificantByte(bytes, byteOrder); isNegative = isBitOn(mostSignificantByte, BITS_PER_BYTE - 1); } return isNegative; } /** * Return the most significant byte in the provided byte array based on endianess. * * @param bytes * @param byteOrder * @return the most significant byte based on endianess. */ private static byte getMostSignificantByte(byte[] bytes, ByteOrder byteOrder) { byte mostSignificantByte = bytes[0]; if (byteOrder == ByteOrder.BIG_ENDIAN) { // do nothing // mostSignificantByte = bytes[0]; } else if (byteOrder == ByteOrder.LITTLE_ENDIAN) { mostSignificantByte = bytes[bytes.length - 1]; } else { throw new IllegalStateException("Unrecognized ByteOrder value[" + byteOrder + "]."); } return mostSignificantByte; } /** * Return true is the bit found at the bitIndex position is on (meaning it has a value of 1). * * @param aByte * @param bitIndex * @return true if the bitIndex position is occupied by a 1. */ public static boolean isBitOn(byte aByte, int bitIndex) { boolean isBitOn = getBitValue(aByte, bitIndex) == 1; return isBitOn; } /** * Return the bit value(0 or 1) for the bit found at the bitIndex position in the provided byte. * * @param aByte * @param bitIndex * @return 0 or 1 based on the bit value at the bitIndex position in the provided byte. */ private static int getBitValue(byte aByte, int bitIndex) { if (bitIndex >= BITS_PER_BYTE) { throw new ArrayIndexOutOfBoundsException("The provided bitIndex[" + bitIndex + "] is larger than the size of a byte[" + BITS_PER_BYTE + "]."); } if (bitIndex < 0) { throw new ArrayIndexOutOfBoundsException( "The provided bitIndex[" + bitIndex + "] must be greater than or equal to zero."); } int value = (aByte >> bitIndex) & 1; return value; } }