Java lang.Long.toBinaryString() method with Examples - GeeksforGeeks

05 Dec, 2018
The java.lang.Long.toBinaryString() method returns a string representation of the long argument as an unsigned integer in base 2. It accepts an argument in Long data-type and returns the corresponding binary string.
Syntax:
public static String toBinaryString(long num)

Parameters : The function accepts a single mandatory parameter: 
num - This parameter specifies the number to be converted to binary string. 
It is of Long data-type.

Return Value: This function returns the string representation of the unsigned Long value represented by the argument in binary (base 2).
Examples:
Input : 10 
Output : 1010 

Input : 9
Output : 1001 

Program 1 : The program below demonstrates the working of function.
// Java program to demonstrate// of java.lang.Long.toBinaryString() methodimport java.lang.Math;  class Gfg1 {      // driver code    public static void main(String args[])    {          long l = 10;        // returns the string representation of the unsigned long value        // represented by the argument in binary (base 2)        System.out.println("Binary is " + Long.toBinaryString(l));          l = 20987752;        System.out.println("Binary is " + Long.toBinaryString(l));    }}
Output:
Binary is 1010
Binary is 1010000000011111101101000

Program 2: The program below demonstrates the working function when a negative number is passed.
// Java program to demonstrate overflow// of java.lang.Long.toBinaryString() methodimport java.lang.Math;  class Gfg1 {      // driver code    public static void main(String args[])    {        // conversion of a negative number to Binary        long l = -13;        System.out.println("Binary is " + Long.toBinaryString(l));    }}
Output:
Binary is 1111111111111111111111111111111111111111111111111111111111110011

Errors and Exceptions: Whenever a decimal number or a string is passed as an argument, it returns an error message which says “incompatible types”.
Program 3: The program below demonstrates the working function when a string number is passed.
// Java program to demonstrate overflow// of java.lang.Long.toBinaryString() methodimport java.lang.Math;  class Gfg1 {      // driver code    public static void main(String args[])    {          // error message thrown when a string is passed        System.out.println("Binary is " + Long.toBinaryString("10"));    }}
Output:
prog.java:12: error: incompatible types: String cannot be converted to long
    System.out.println("Binary is " + Long.toBinaryString("10"));
Program 4: The program below demonstrates the working function when a decimal is passed.
// Java program to demonstrate overflow// of java.lang.Long.toBinaryString() methodimport java.lang.Math;  class Gfg1 {      // driver code    public static void main(String args[])    {          // error message thrown when a decimal is passed        System.out.println("Binary is " + Long.toBinaryString(10.25));    }}
Output:
prog.java:12: error: incompatible types: possible lossy conversion from double to long
    System.out.println("Binary is " + Long.toBinaryString(10.25));
Java-Functions
Java-lang package
java-Long
java-math
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
LinkedList in Java