- On of the way to convert decimal number to its binary representation using the Integer.toBinaryString() method. This method takes an integer as an argument and returns its binary representation as a string. Here is an example:
public static String decimalToBinary(int decimal) {
return Integer.toBinaryString(decimal);
}
We can call this method and pass a decimal number as an argument, and it will return the binary representation of that number. For example:
int decimal = 10;
String binary = decimalToBinary(decimal);
System.out.println(binary); // Output: 1010
In the above method will return a string representation of the binary number, if we want to have it as an integer you can use Integer.parseInt(binary, 2) to convert it to an int.
Another way to convert a decimal number to its binary representation is by using bit shifting and bitwise operations. You can repeatedly divide the decimal number by 2 and keep track of the remainder. The remainder represents the least significant bit of the binary representation. You can then reverse the order of the remainders to get the binary representation of the decimal number.
In summary, In Java you can use the Integer.toBinaryString() method to convert a decimal number to its binary representation as a string, and you can use Integer.parseInt(binary, 2) to convert it to an int. Also, you can use bit shifting and bitwise operations to convert a decimal number to its binary representation.
Another way to convert a decimal number to its binary representation is by using bit shifting and bitwise operations.
This method involves repeatedly dividing the decimal number by 2 and keeping track of the remainders. The remainders represent the least significant bits of the binary representation.
public static String decimalToBinaryUsingBitOperation(int decimal) {
StringBuilder binary = new StringBuilder();
while (decimal > 0) {
binary.append(decimal % 2);
decimal = decimal >> 1;
}
return binary.reverse().toString();
}
the function uses a while loop to divide the decimal number by 2 and keep track of the remainders. The remainders are appended to a StringBuilder object. The while loop continues until the decimal number is 0. At the end of the loop, the binary representation of the decimal number is obtained by reversing the order of the remainders using the reverse() method of the StringBuilder. The binary representation is then returned as a string.
This method will also work for negative decimal numbers, but the result will be the two's complement representation of the negative number, it will have a leading 1 in the most significant bit.
It's worth noting that, the above method will return the binary representation as a string, if you want to have it as an integer you can use Integer.parseInt(binary, 2) to convert it to an int.
In summary, there are different ways to convert a decimal number to its binary representation in Java. One way is to use the Integer.toBinaryString() method, another way is by using bit shifting and bitwise operations, this method uses a while loop to repeatedly divide the decimal number by 2 and keep track of the remainders, which represent the binary representation of the decimal number. The result can be returned as a string or as an integer by using the Integer.parseInt(binary, 2)
- We can convert binary to decimal in three ways
1.Using Integer.toBinaryString(int num);
2.Using Stack
3.Using Custom logic
1.Write a Java Program to convert decimal to binary in java using Integer.toBinaryString(int num)
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- System.out.println("\nBinary representation of 1: ");
- System.out.println(Integer.toBinaryString(1));
- System.out.println("\nBinary representation of 4: ");
- System.out.println(Integer.toBinaryString(4));
- System.out.println("Binary representation of 10: ");
- System.out.println(Integer.toBinaryString(10));
- System.out.println("\nBinary representation of 12: ");
- System.out.println(Integer.toBinaryString(12));
- System.out.println("\nBinary representation of 120: ");
- System.out.println(Integer.toBinaryString(120));
- System.out.println("\nBinary representation of 500: ");
- System.out.println(Integer.toBinaryString(500));
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
- Binary representation of 120:
- 1111000
- Binary representation of 500:
- 111110100
2.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- import java.util.Stack;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Create Stack object
- Stack<Integer> stack = new Stack<Integer>();
- //Take User input from keyboard
- System.out.println("Enter decimal number: ");
- int num = in.nextInt();
- while (num != 0)
- {
- int d = num % 2;
- stack.push(d);
- num /= 2;
- }
- System.out.print("\nBinary representation is:");
- while (!(stack.isEmpty() ))
- {
- System.out.print(stack.pop());
- }
- System.out.println();
- }
- }
- }
Output:
- Enter decimal number:
- 12
- Binary representation is:1100
3.Write a Java Program to convert decimal to binary in java
- import java.util.Scanner;
- public class ConvertDecimalToBinary{
- /**
- *www.instanceofjava.com
- */
- public static void convertDeciamlToBinary(int num){
- int binary[] = new int[40];
- int index = 0;
- while(num > 0){
- binary[index++] = num%2;
- num = num/2;
- }
- for(int i = index-1;i >= 0;i--){
- System.out.print(binary[i]);
- }
- }
- public static void main(String[] args) {
- System.out.println("Binary representation of 1: ");
- convertDeciamlToBinary(1);
- System.out.println("\nBinary representation of 4: ");
- convertDeciamlToBinary(4);
- System.out.println("\nBinary representation of 10: ");
- convertDeciamlToBinary(10);
- System.out.println("\nBinary representation of 12: ");
- convertDeciamlToBinary(12);
- }
- }
Output:
- Binary representation of 1:
- 1
- Binary representation of 4:
- 100
- Binary representation of 10:
- 1010
- Binary representation of 12:
- 1100
You Might Like:
Java Example Program to convert binary to decimal
Java programming interview questions
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
No comments