Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java lang.Long.toBinaryString() method with Examples
The java.lang.Long.toBinaryString() method returns a string representation of the long argument as an unsigned integer in base 2.
Example
Following is an example to implement the toBinaryString() method −
import java.lang.*;
public class Demo {
public static void main(String[] args) {
long l = 190;
System.out.println("Number = " + l);
/* 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));
// returns the number of one-bits
System.out.println("Number of one bits = "
}
}
Output
Number = 190 Binary is 10111110 Number of one bits = 6
Example
Let us see another example wherein we are considering negative number −
import java.lang.*;
public class Demo {
public static void main(String[] args) {
long l = -25;
System.out.println("Number = " + l);
System.out.println("Binary is " + Long.toBinaryString(l));
System.out.println("Number of one bits = " + Long.bitCount(l));
}
}
Output
Number = -25 Binary is 1111111111111111111111111111111111111111111111111111111111100111 Number of one bits = 62
Advertisements