Converts a 2x2 matrix of nibbles to a 16-bit short. - Java java.lang

Java examples for java.lang:Math Convert

Description

Converts a 2x2 matrix of nibbles to a 16-bit short.

Demo Code

/** Copyright 2014 Noel Niles
 * /*from w ww.  j  a  v a  2  s  . c  om*/
 * This file is part of SAES.
 *
 * S-AES 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/>.
 ******************************************************************************/
//package com.java2s;

public class Main {
    /** Converts a 2x2 matrix of nibbles to a 16-bit short.
     * 
     * Operates column wise. Example:
     * 
     *                      | 0x01 0x03 |                     
     *                      | 0x02 0x04 | == 0x1234
     * 
     * @param b: 2x2 array.
     * @return result: a short
     **************************************************************************/
     static short matrixToShort(final byte[][] b) {
        short result = 0;
        result = (short) (result | b[0][0]);
        result = (short) ((result << 4) | b[1][0]);
        result = (short) ((result << 4) | b[0][1]);
        result = (short) ((result << 4) | b[1][1]);
        return result;
    }
}

Related Tutorials