Java Array Shift shiftNibbles(byte[] bytes)

Here you can find the source of shiftNibbles(byte[] bytes)

Description

Shifts every nibble pair of every byte

License

Open Source License

Parameter

Parameter Description
bytes The byte array

Return

A byte[] with each byte in the same position with nibbles reversed

Declaration

public static byte[] shiftNibbles(byte[] bytes) 

Method Source Code

//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);
    }
}

Related

  1. shiftLeftAndFill(byte[] array, int positions)
  2. shiftLeftByCopying(int[] table, int shift)
  3. shiftLeftByOne(int[] table)
  4. shiftLeftI(long[] v, int off)
  5. ShiftLeftOne(int[] arr)
  6. shiftOff(T[] a)
  7. shiftOffsets(int[] offsets, int changeOffset, int oldLength, int newLength)
  8. shiftOnRow(double[][] d, int q)
  9. shiftRight(byte[] block)