As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted to a decimal number and vice versa. In java there are 4 types of numbers:
Types Of Numbers | Base |
---|---|
Binary | 2 |
Octa-Decimal | 8 |
Decimal | 10 |
Hexadecimal | 16 |
Approaches: In Java, there are 2 ways to convert either using pre-defined methods or third-grade logic building as listed below:
- Using Integer.toBinaryString method (Integer wrapper class)
- Using the Brute force method (without any use of predefined classes)
Examples:
Input. 1: 10 Output 1: The binary equivalent of 10 is : 1010 Number of 1s is : 2 Input 2: 15 Output 2: The binary equivalent of 15 is : 1111 Number of 1s is 4
Approach 1: Using toBinaryString() Method: represents the number to be converted into binary. The Integer class of java provides some useful methods to deal with Integers. One such method is Integer.toBinaryString(int x)
Syntax:
public static String toBinaryString(int variable_name)
Parameters: Decimal integer to be converted.
Return Type: String which is holding binary representation of the integer converted or simply binary equivalent of integer as a String object.
Exceptions: There are no exceptions thrown by this method
Parameter: It takes one parameter of type Integer (or int)
Implementation: To count the number of 1s, check if each character of the obtained binary string equals 1 or not.
Java
// Java program to convert decimal to binary number // and counting number of 1's in it public class GFG { // Function to convert decimal to binary void convertAndCount( int num) { // Store the count of 1s currently 0 int count = 0 ; // Returns a String object representing // binary equivalent of passed decimal number String binary = Integer.toBinaryString(num); // Iterating over obtained string using length // function for ( int i = 0 ; i < binary.length(); i++) // Checking 1 is present in binary if (binary.charAt(i) == '1' ) // Increment the count // if any char equals 1 count++; // Printing the binary equivalent System.out.println( "The binary equivalent of " + num + " is : " + binary); // Printing the no of 1 in above binary number System.out.println( "Number of 1s is : " + count); } // Main driver method public static void main(String[] args) { // Creating object in main GFG obj = new GFG(); // Calling the convertAndCount() method // over the integer value 18 obj.convertAndCount( 18 ); } } |
The binary equivalent of 18 is : 10010 Number of 1s is : 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 2:Without using pre-defined variables
- Dividing the decimal number by 2 which is to be converted into binary
- Storing the remainder
Java
// Java program to convert decimal to binary number // and counting number of 1's in it public class GFG { // Function to convert decimal to binary void convertAndCount( int num) { int temp = num; // a temporary variable to store the // value of num // to store the binary value int [] binary = new int [ 20 ]; // to store the count of 1s int count = 0 ; int i; // to iterate through the loop and keep a // count of no.of digits in the obtained binary for (i = 0 ; temp > 0 ; i++) { // divide the number by 2 // and store the remainder temp /= 2 ; binary[i] = temp % 2 ; // If 1 is present in binary if (binary[i] == 1 ) // increment the count if any digit // is equal to 1 count++; } // Printing binary of decimal number System.out.print( "The binary equivalent of " + num + " is : " ); // Iterating over array for ( int j = i - 1 ; j >= 0 ; j--) // Printing obtained array in reverse order System.out.print(binary[j]); // Printing number of 1's System.out.println( "\nNumber of 1s is : " + count); } // Main driver method public static void main(String[] args) { // Creating class GFG object in main GFG obj = new GFG(); obj.convertAndCount( 18 ); // calling convertAndCount() method on decimal // over the value 18 } } |
The binary equivalent of 18 is : 01001 Number of 1s is : 2
Time complexity: O(n) for given number n
Auxiliary space: O(1)