我有一个数字,想要将它以二进制形式打印出来。我不想通过编写算法来实现。
在Java中是否有内置函数可以实现这个功能?
我有一个数字,想要将它以二进制形式打印出来。我不想通过编写算法来实现。
在Java中是否有内置函数可以实现这个功能?
假设你的意思是“内置的”:
int x = 100;
System.out.println(Integer.toBinaryString(x));
请查看Integer文档。(Long
具有类似的方法,BigInteger
具有一个实例方法,您可以在其中指定基数。)
在这里,不需要仅依赖于二进制或其他任何格式...一个灵活的内置函数可用,它可以打印您在程序中想要的任何格式。 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
toBinaryString
方法输出二进制补码形式,toString
方法将数字转换为指定进制并在前面加上负号,因此 toString(-8, 2)
的结果为 "-1000"。请注意,这里的负号是在转换后添加的。 - JoeSystem.out.println(Integer.toBinaryString(343));
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));
}
result.replace(result.length() - 1, result.length(), "");
?因为我们传递的是已经是32位的int
。在倒数第二行我们要替换什么? - ParthString
类的trim
函数来实现相同的效果。 - Maghoumiif (i%groupSize == 0 && i!= 0)
。 - Ilya Serbislong v = value; if (value < 0) v += (1L << 32);
- Infernozpublic 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));
输出(最低有效位在0位置):
0 0 1 0 2 1 3 1 4 1 5 0 6 0 7 0
这是打印整数内部二进制表示最简单的方法。 例如:如果我们取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());
if(a==0){ binary.append(0); System.out.println(binary.toString()); return; }
。 - Imran简单易行,非常简单的解决方案。
public static String intToBinaryString(int integer, int numberOfBits) {
if (numberOfBits > 0) { // To prevent FormatFlagsConversionMismatchException.
String nBits = String.format("%" + numberOfBits + "s", // Int to bits conversion
Integer.toBinaryString(integer))
.replaceAll(" ","0");
return nBits; // returning the Bits for the given int.
}
return null; // if the numberOfBits is not greater than 0, returning null.
}
return null
。请使用守卫语句抛出 IllegalArgumentException
异常。在任何错误情况下返回 null
都是一种糟糕的构造方式,只会将问题推给调用者。 - Maarten Bodewes只需尝试一下。如果范围仅限于打印给定整数值的二进制值。它可以是正数或负数。
public static void printBinaryNumbers(int n) {
char[] arr = Integer.toBinaryString(n).toCharArray();
StringBuilder sb = new StringBuilder();
for (Character c : arr) {
sb.append(c);
}
System.out.println(sb);
}
input
5
Output
101
toBinaryString
ŤŅĒŚõěÁöĄŚ≠óÁ¨¶šł≤Ôľü - OneCricketeer