Java.lang Package Classes

Java Long - toBinaryString() Method



The java.lang.Long.toBinaryString() method returns a string representation of the long argument as an unsigned integer in base 2.

Syntax

public static String toBinaryString(long i)

Parameters

i Specify a long to be converted to a string.

Return Value

Returns the string representation of the unsigned long value represented by the argument in binary (base 2).

Exception

NA.

Example:

In the example below, the java.lang.Long.toBinaryString() method returns a string representation of the long argument as an unsigned integer in base 2.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating long value
    long x = 25;
    long y = 31;
    long z = 111;

    //creating and printing string representation of 
    //argument in binary system.
    System.out.println("x in binary system is: " + Long.toBinaryString(x)); 
    System.out.println("y in binary system is: " + Long.toBinaryString(y)); 
    System.out.println("z in binary system is: " + Long.toBinaryString(z));   
  }
}

The output of the above code will be:

x in binary system is: 11001
y in binary system is: 11111
z in binary system is: 1101111

❮ Java.lang - Long