Here you can find the source of shiftNibbles(byte[] bytes)
Parameter | Description |
---|---|
bytes | The byte array |
public static byte[] shiftNibbles(byte[] bytes)
//package com.java2s; //License from project: Open Source License public class Main { /** Most significant bits mask for binary operations */ public static final int MOST_SIGNIFICANT_MASK = 0xFF; /** Least significant bits mask for binary operations */ public static final int LEAST_SIGNIFICANT_MASK = 0x0F; /**//from w w w . jav a 2 s . co m * Shifts the nibble pair positions in a {@code byte} * @param b The encoded {@code byte} * @return A {@code byte} with shifted nibbles */ public static byte shiftNibbles(byte b) { // Acquires the least significant half and shifts right byte leastSignificantHalf = (byte) (leastSignificantNibble(b) << 4); byte mostSignificantHalf = mostSignificantNibble(b); return (byte) (leastSignificantHalf | mostSignificantHalf); } /** * Shifts every nibble pair of every byte * @see {@link NibbleUtils#shiftNibbles(byte)} * @param bytes The byte array * @return A {@code byte[]} with each byte in the same position with nibbles reversed */ public static byte[] shiftNibbles(byte[] bytes) { assert (bytes != null); assert (bytes.length > 0); byte[] bytesRet = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) bytesRet[i] = shiftNibbles(bytes[i]); return bytesRet; } /** * Shifts nibble pairs of the specified * @see {@link NibbleUtils#shiftNibbles(byte)} * @param bytes The byte array * @param start Start index, inclusive (first index is 1) * @param end The end index, inclusive (last index is length) * @return A {@code byte[]} with specified bytes reversed. Bytes order are kept */ public static byte[] shiftNibbles(byte[] bytes, int start, int end) { assert (bytes != null); assert (bytes.length > 0); byte[] bytesRet = new byte[bytes.length]; // Only shifts specified bytes for (int i = 0; i < bytes.length; i++) if (i >= start - 1 && i < end) bytesRet[i] = shiftNibbles(bytes[i]); else bytesRet[i] = bytes[i]; return bytesRet; } /** * Extracts the 4 least significant bits * @param b The byte * @return A byte with 0b0000 plus the 4 least significant bits */ public static byte leastSignificantNibble(byte b) { return (byte) (b & LEAST_SIGNIFICANT_MASK); } /** * Extracts the 4 most significant bits * @param b The byte * @return A byte with 0b0000 plus the 4 most significant bits */ public static byte mostSignificantNibble(byte b) { return (byte) ((b & MOST_SIGNIFICANT_MASK) >>> 4); } }