Java Online Compiler
Example: Java program to convert decimal to binary using bitwise operator
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Java program to convert decimal to binary using bitwise operator public class Main { public static void main(String[] args) { // Step-1 Declare and initialize the decimal number int num = 10; // Step-2 Check if the number is zero if (num == 0) { System.out.println("Binary representation: 0"); return; } // Step-3 Store result using bitwise operations String binary = ""; for (int i = 31; i >= 0; i--) { int bit = (num >> i) & 1; binary += bit; } // Step-4 Trim leading zeros binary = binary.replaceFirst("^0+(?!$)", ""); // Step-5 Output result System.out.println("Binary representation: " + binary); } }
Output
Clear
ADVERTISEMENTS