一个short型值用十六位比特存储。编写程序,提示用户,输入一个短整形,然后显示这个整数的16比特形式运行示例
Enter an integer :5
the bits is 0000000000000101
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void show(int n, int acc)
{
if (n < 0) n = 65536 + n;
if (acc == 0) return;
show(n >> 1, acc - 1);
System.out.print(n % 2);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.print("5 the bits is ");
show(5, 16);
System.out.println();
System.out.print("-5 the bits is ");
show(-5, 16);
System.out.println();
}
}
String s = Integer.toHexString(5);
while (s.length < 16)
s = "0" + s;
5 the bits is 0000000000000101
-5 the bits is 1111111111111011