Output binary representation

Problem Description
  input integer [-128,127] therein, and outputs the binary representation. Tips can be used with bit &
input formats
  Example: 7
Output Format
  Example: 00000111
Sample input
an input example to meet the requirements of the subject.
Example:
7
sample output
corresponding to the output of the input sample above.
Example:
00000111
size of the data and the agreed
  input data range of each number.
  Example: 0 <n, m <100 , 0 < = the number of each matrix <= 1000.
 
  The problem is not expressed in detail

在这里插入代码片
package Main;

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String []args)
	{Scanner input= new Scanner(System.in);
	int n= input.nextInt();
	int v=n;
	String s="";
for(int i=0;i<7;i++)
{s=(n&1)+s;
n=n>>1;
}
if(v>=0)
System.out.print(0+s);
else
	System.out.print(1+s);	

	}
	
	
}

Outer alternative API approach

在这里插入代码片
public void function1(int n){
     String result = Integer.toBinaryString(n);
     //int r = Integer.parseInt(result);
     //System.out.println(r);
     System.out.println(result);
 }
Published 130 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/104596630