Sunday 5 January 2020

How to print integer in binary format?

‘Integer.toBinaryString’ method is used to convert integer to binary string.

Example
int i = 1027;
String binaryString = Integer.toBinaryString(i);

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  int i = 1027;

  String binaryString = Integer.toBinaryString(i);
  
  System.out.println("Binary form of " + i + " is " + binaryString);

 }
}

Output
Binary form of 1027 is 10000000011

You may like

No comments:

Post a Comment