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

Java examples for java.lang:int Binary

Description

Interpret a long as its binary form

Demo Code

/*// w ww.j ava2 s .  com
 * 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 {
        long longValue = 2;
        System.out.println(java.util.Arrays.toString(fromLong(longValue)));
    }

    /**
     * Interpret a long as its binary form
     *
     * @param longValue The long to interpret to binary
     *
     * @return The binary
     */
    public static byte[] fromLong(long longValue) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (longValue >> 56);
        bytes[1] = (byte) ((longValue << 8) >> 56);
        bytes[2] = (byte) ((longValue << 16) >> 56);
        bytes[3] = (byte) ((longValue << 24) >> 56);
        bytes[4] = (byte) ((longValue << 32) >> 56);
        bytes[5] = (byte) ((longValue << 40) >> 56);
        bytes[6] = (byte) ((longValue << 48) >> 56);
        bytes[7] = (byte) ((longValue << 56) >> 56);
        return bytes;
    }
}

Related Tutorials