Jon*_*eet 410
假设你的意思是"内置":
int x = 100;
System.out.println(Integer.toBinaryString(x));
请参阅整数文档.
(Long有一个类似的方法,BigInteger有一个实例方法,你可以指定基数.)
Moh*_*Ali 238
这里不需要只依赖于二进制或任何其他格式......一个灵活的内置函数可用于打印你想要的程序格式.Integer.toString(int,representation);
Integer.toString(100,8) // prints 144 --octal representation
Integer.toString(100,2) // prints 1100100 --binary representation
Integer.toString(100,16) //prints 64 --Hex representation
ada*_*shr 61
System.out.println(Integer.toBinaryString(343));
M2X*_*M2X 21
我需要一些东西来很好地打印出来并将每个n位的位分开.换句话说,显示前导零并显示如下内容:
n = 5463
output = 0000 0000 0000 0000 0001 0101 0101 0111
所以这就是我写的:
/**
 * Converts an integer to a 32-bit binary string
 * @param number
 *      The number to convert
 * @param groupSize
 *      The number of bits in a group
 * @return
 *      The 32-bit long bit string
 */
public static String intToString(int number, int groupSize) {
    StringBuilder result = new StringBuilder();
    for(int i = 31; i >= 0 ; i--) {
        int mask = 1 << i;
        result.append((number & mask) != 0 ? "1" : "0");
        if (i % groupSize == 0)
            result.append(" ");
    }
    result.replace(result.length() - 1, result.length(), "");
    return result.toString();
}
像这样调用它:
public static void main(String[] args) {
    System.out.println(intToString(5463, 4));
}
Sey*_*lal 10
public static void main(String[] args) 
{
    int i = 13;
    short s = 13;
    byte b = 13;
    System.out.println("i: " + String.format("%32s", 
            Integer.toBinaryString(i)).replaceAll(" ", "0"));
    System.out.println("s: " + String.format("%16s", 
            Integer.toBinaryString(0xFFFF & s)).replaceAll(" ", "0"));
    System.out.println("b: " + String.format("%8s", 
            Integer.toBinaryString(0xFF & b)).replaceAll(" ", "0"));
}
输出:
i: 00000000000000000000000000001101
s: 0000000000001101
b: 00001101
老套:
    int value = 28;
    for(int i = 1, j = 0; i < 256; i = i << 1, j++)
        System.out.println(j + " " + ((value & i) > 0 ? 1 : 0));
小智 6
这是打印整数内部二进制表示的最简单方法。 例如:如果我们将 n 设为 17,那么输出将是:0000 0000 0000 0000 0000 0000 0001 0001
void bitPattern(int n) {
        int mask = 1 << 31;
        int count = 0;
        while(mask != 0) {
            if(count%4 == 0)
                System.out.print(" ");
            if((mask&n) == 0) 
                System.out.print("0");
            else 
                System.out.print("1");
            count++;
            mask = mask >>> 1;
    }
    System.out.println();
}
检查出这个逻辑可以将数字转换为任何基数
public static void toBase(int number, int base) {
    String binary = "";
    int temp = number/2+1;
    for (int j = 0; j < temp ; j++) {
        try {
            binary += "" + number % base;
            number /= base;
        } catch (Exception e) {
        }
    }
    for (int j = binary.length() - 1; j >= 0; j--) {
        System.out.print(binary.charAt(j));
    }
}
要么
StringBuilder binary = new StringBuilder();
int n=15;
while (n>0) {
    if((n&1)==1){
        binary.append(1);
    }else
        binary.append(0);
    n>>=1;
}
System.out.println(binary.reverse());
| 归档时间: | 
 | 
| 查看次数: | 330874 次 | 
| 最近记录: |