Interpret a short as its binary form - Java java.lang

Java examples for java.lang:int Binary

Description

Interpret a short as its binary form

Demo Code

/*//w w  w .j  a va 2s  . co m
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int shortValue = 2;
        System.out
                .println(java.util.Arrays.toString(fromShort(shortValue)));
    }

    /**
     * Interpret a short as its binary form
     *
     * @param shortValue The short to interpret to binary
     *
     * @return The binary
     */
    public static byte[] fromShort(int shortValue) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (shortValue >> 8);
        bytes[1] = (byte) ((shortValue << 8) >> 8);
        return bytes;
    }
}

Related Tutorials