Java Binary Encode toBinaryString(int b, int bits)

Here you can find the source of toBinaryString(int b, int bits)

Description

to Binary String

License

Open Source License

Declaration

private static String toBinaryString(int b, int bits) 

Method Source Code

//package com.java2s;
/**//  w  ww  .  jav  a2s .  c  om
 *
 * Created: Feb 26, 2010
 *
 * Part of Frida IRE.
 * Copyright (c) 2010, Karl Trygve Kalleberg, Ole Andr? Vadla Ravn?s
 * Licensed under the GNU General Public License, v3
 *
 *
 * @author: karltk@boblycat.org
 */

public class Main {
    private static String toBinaryString(int b, int bits) {
        int v = 1 << (bits - 1);
        String s = "";
        for (int i = 0; i < bits; i++) {
            if ((b & v) == v) {
                s += "1";
            } else {
                s += "0";
            }
            v >>= 1;
        }
        return s;
    }

    public static String toBinaryString(int b) {
        return toBinaryString(b, 32);
    }

    public static String toBinaryString(short b) {
        return toBinaryString(b, 16);
    }

    public static String toBinaryString(byte b) {
        return toBinaryString(b, 8);
    }
}

Related

  1. toBinaryString(char value)
  2. toBinaryString(final boolean[] bits)
  3. toBinaryString(final byte number)
  4. toBinaryString(final char[] arr)
  5. toBinaryString(int a, int bits)
  6. toBinaryString(int l, int L)
  7. toBinaryString(int number)
  8. toBinaryString(int number, int length)
  9. toBinaryString(int value)