Java Binary Encode toBinaryString(long val)

Here you can find the source of toBinaryString(long val)

Description

to Binary String

License

Open Source License

Declaration

public static CharSequence toBinaryString(long val) 

Method Source Code

//package com.java2s;
/**/*  ww  w . j a v a2 s  .co m*/
 * File: $HeadURL: https://hdt-java.googlecode.com/svn/trunk/hdt-java/src/org/rdfhdt/hdt/util/io/IOUtil.java $
 * Revision: $Rev: 194 $
 * Last modified: $Date: 2013-03-04 21:30:01 +0000 (lun, 04 mar 2013) $
 * Last modified by: $Author: mario.arias $
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contacting the authors:
 *   Mario Arias:               mario.arias@deri.org
 *   Javier D. Fernandez:       jfergar@infor.uva.es
 *   Miguel A. Martinez-Prieto: migumar2@infor.uva.es
 *   Alejandro Andres:          fuzzy.alej@gmail.com
 */

public class Main {
    public static CharSequence toBinaryString(long val) {
        StringBuilder str = new StringBuilder(64);
        int bits = 64;
        while (bits-- != 0) {
            str.append(((val >>> bits) & 1) != 0 ? '1' : '0');
        }
        return str;
    }

    public static CharSequence toBinaryString(int val) {
        StringBuilder str = new StringBuilder(32);
        int bits = 32;
        while (bits-- != 0) {
            str.append(((val >>> bits) & 1) != 0 ? '1' : '0');
        }
        return str;
    }
}

Related

  1. toBinaryString(int l, int L)
  2. toBinaryString(int number)
  3. toBinaryString(int number, int length)
  4. toBinaryString(int value)
  5. toBinaryString(int[] input)
  6. toBinaryString(long value, int bitLegnth)
  7. toBinaryStringAddress(long address)
  8. toBinaryText(StringBuffer buf)
  9. toBinFromByte(final byte b)