64 Bit Decimal to Binary Converter In Java

Hey, Have you ever think about the simple basic things, the Binary. Yeah , whatever we do in any digital electronics device all are processed and stored in memory in binary. Its a basic.
The conversion of decimal to binary.... Yeah, we all know about it. Its just division and reminder.. and repeat..
But how we efficiently do that... And we have limitation.. Of-course we have..

Even computers have..
A 64 bit binary in Decimal is around 10 to the power 19... yeah very large enough... Now we convert Decimal to Binary.. lets check the limitation...

Screenshot / Output



Showing the bit length and the binary equivalent of the decimal input


The Code:
import java.util.*;

public class binary
{
 public static void main(String arg[])
 {
  System.out.print("Give the decimal Number:  ");
  Scanner s = new Scanner(System.in);
  long n = s.nextLong();
  long a[] = new long[64];
  int i,j,k;
  for(i = 0; i<64; i++)
  {
   a[63-i] = (n%2);
   n=n/2;
  }
  for(k=0; k<64; k++)
  {
   if(a[k]==1)
   break;
  }
  System.out.println("Number of bit" +(64-k));
  for(int l=k; l>=k&&l<64; l++)
  {
   System.out.print(a[l]);
  }
  /*System.out.println("  "); 
  for(j=0; j<64; j++)
  {
   System.out.print(a[j]);
  }*/
 }
}

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Employee Management System Using Inheritance in Java

Bit Stuffing Code Implementation in Java