Java Bit Print printBits(long value)

Here you can find the source of printBits(long value)

Description

Print the bits in the value with the leftmost bit being the most significant bit.

License

LGPL

Parameter

Parameter Description
value a <code>long</code> value

Return

a String value

Declaration

public static String printBits(long value) 

Method Source Code

//package com.java2s;
/*//ww w. j av  a 2s .co m
 * Copyright (c) 2013 mellowtech.org.
 *
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 * not use this file except in compliance with one of these Licenses.
 *
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 *
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.opensource.org/licenses/lgpl-3.0
 *
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.opensource.org/licenses/lgpl-2.1
 *
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.opensource.org/licenses/cddl1
 *
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 *
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 */

public class Main {
    /**
     * Print the bits in the value with the leftmost bit being the most
     * significant bit.
     * 
     * @param value
     *          a <code>long</code> value
     * @return a <code>String</code> value
     */
    public static String printBits(long value) {
        StringBuffer sb = new StringBuffer();
        for (int shift = 63; shift >= 0; shift--)
            sb.append((((value >>> shift) & 01) != 0) ? "1" : "0");
        return sb.toString();
    }

    /**
     * Print the bits in the value with the leftmost bit being the most
     * significant bit.
     * 
     * @param value
     *          an <code>int</code> value
     * @return a <code>String</code> value
     */
    public static String printBits(int value) {
        StringBuffer sb = new StringBuffer();
        for (int shift = 31; shift >= 0; shift--)
            sb.append((((value >>> shift) & 01) != 0) ? "1" : "0");
        return sb.toString();
    }

    /**
     * Print the bits in the value with the leftmost bit being the most
     * significant bit.
     * 
     * @param value
     *          a <code>short</code> value
     * @return a <code>String</code> value
     */
    public static String printBits(short value) {
        StringBuffer sb = new StringBuffer();
        for (int shift = 15; shift >= 0; shift--)
            sb.append((((value >>> shift) & 01) != 0) ? "1" : "0");
        return sb.toString();
    }

    /**
     * Print the bits in the value with the leftmost bit being the most
     * significant bit.
     * 
     * @param value
     *          a <code>byte</code> value
     * @return a <code>String</code> value
     */
    public static String printBits(byte value) {
        StringBuffer sb = new StringBuffer();
        for (int shift = 7; shift >= 0; shift--)
            sb.append((((value >>> shift) & 01) != 0) ? "1" : "0");
        return sb.toString();
    }

    /**
     * Print the bits in the value with the leftmost bit being the most
     * significant bit.
     * 
     * @param values
     *          a <code>byte</code> value
     * @return a <code>String</code> value
     */
    public static String printBits(byte[] values) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < values.length; i++)
            sb.append(printBits(values[i]) + " ");
        return sb.toString();
    }
}

Related

  1. printBitLong(long A)
  2. printBits(byte[] bytes)
  3. printBits(int meta)
  4. printBits(int number, int n)
  5. printBits(long val, int bits)