How to use  Bitwise Operators In Java

We can use bitwise operators to do the above job. 

Note – Bitwise operators work faster than arithmetic operators used above.

Java Program to Convert Decimal Number to Binary Using Bitwise Operators

Java




// Java program to Decimal to binary conversion
// using bitwise operator
// Size of an integer is assumed to be 32 bits
  
class gfg {
    // Function that convert Decimal to binary
    public void decToBinary(int n)
    {
        // Size of an integer is assumed to be 32 bits
        for (int i = 31; i >= 0; i--) {
            int k = n >> i;
            if ((k & 1) > 0)
                System.out.print("1");
            else
                System.out.print("0");
        }
    }
}
  
class geek {
    // driver code
    public static void main(String[] args)
    {
        gfg g = new gfg();
        int n = 32;
          System.out.println("Decimal - " + n);
         System.out.print("Binary - ");
        g.decToBinary(n);
    }
}


Output

Decimal - 32
Binary - 00000000000000000000000000100000

The complexity of the above method:

Time Complexity: O(1)
Auxiliary Space: O(1)

Java Program for Decimal to Binary Conversion

Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number.

Examples: 

Input : 7
Output : 111

Input: 33
Output: 100001

Binary-to-decimal conversion is done to convert a number given in the binary system to its equivalent in the decimal number system. A number system is a format to represent numbers in a certain way. 

Binary Number System – The binary number system is used in computers and electronic systems to represent data, and it consists of only two digits which are 0 and 1. 

Decimal Number System – The decimal number system is the most commonly used number system worldwide, which is easily understandable to people. It consists of digits from 0 to 9.

Similar Reads

Methods For Decimal to Binary Conversion

There are numerous approaches to converting the given decimal number into an equivalent binary number in Java. A few of them are listed below....

1. Using Arrays

Algorithm...

2. Using  Bitwise Operators

...

3. Using Math.pow() method (Without using Arrays)

We can use bitwise operators to do the above job....

Contact Us