2013년 8월 5일 월요일

JAVA Decimal to binary(10진수를 2진수로 바꾸는 예제)

자바에서 십진수를 이진수로 바꾸는 예제 입니다.


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의) 
 www.onjprogramming.co.kr




//10진수를 이진수로...
class DecimalToBinary {
 int[] binary(int value) { 
  int n, i;
  int[] b = new int[64];
  for(i=63; value>1; i--) {
   //나머지
   n = value % 2;
   //몫
   value = value / 2;
   if (value < 2){
    b[i] = n;
    b[i-1] = value;
   }
   else {
    b[i] = n;
   }
  }
  b[0] = i;
  return b;
 }
 public static void main(String[] args) {
  long time = System.currentTimeMillis();
  if (args.length<1) {
   System.out.println("Usage : java DecimalToBinary number ");
   System.exit(1);
  }
        int value = Integer.parseInt(args[0]);
  DecimalToBinary d = new DecimalToBinary();
  int[] b = d.binary(value);
 
  System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
  for(int j=b[0]; j < 64; j++) {
   System.out.print(b[j]);
  }
  System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
  Runtime r = Runtime.getRuntime();
  long t = r.totalMemory();
  long f = r.freeMemory();
  System.out.println("총 메모리 : " + t + " bytes");
  System.out.println("남은 메모리 : " + f  + "bytes");
  System.out.println("");
 }
}
 
 
 
 
--------------------------------------------------
 
 
 
 
//10진수를 이진수로...
class DecimalToBinary1 {
 int[] binary(int value) { 
  int i=63;
  int[] b = new int[64];
  while(value > 0){ //convert decimal to binary representation
    if((value %2)!=0)  b[i]=1;
    i--;
    value>>=1;   //divide decimal by 2
  }
  b[0] = i+1;
  return b;
 }
 public static void main(String[] args) {
  long time = System.currentTimeMillis();
  if (args.length<1) {
   System.out.println("Usage : java DecimalToBinary number ");
   System.exit(1);
  }
        int value = Integer.parseInt(args[0]);
  DecimalToBinary1 d = new DecimalToBinary1();
  int[] b = d.binary(value);
 
  System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
  for(int j=b[0]; j < 64; j++) {
   System.out.print(b[j]);
  }
  System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
  Runtime r = Runtime.getRuntime();
  long t = r.totalMemory();
  long f = r.freeMemory();
  System.out.println("총 메모리 : " + t + " bytes");
  System.out.println("남은 메모리 : " + f  + "bytes");
  System.out.println("");
 }
}
 
 
 
 
 
 
 
--------------------------------------------------------------------
 
 
 
//10진수를 이진수로...
class DecimalToBinary2 {
 int i=63;
 static int[] b = new int[64]; 
 void binary(int value) {   
  if (value > 1){
   b[i] = value % 2;
   i--;
   binary(value/2);
  }  
  else {
   b[i] = value;
   b[0] = i;
  } 
 }
 public static void main(String[] args) {
  long time = System.currentTimeMillis();
  if (args.length<1) {
   System.out.println("Usage : java DecimalToBinary number ");
   System.exit(1);
  }
        int value = Integer.parseInt(args[0]);
  DecimalToBinary2 d = new DecimalToBinary2();
  d.binary(value);
 
  System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
  for(int j=b[0]; j < 64; j++) {
   System.out.print(b[j]);
  }
  System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
  Runtime r = Runtime.getRuntime();
  long t = r.totalMemory();
  long f = r.freeMemory();

  System.out.println("총 메모리 : " + t + " bytes");
  System.out.println("남은 메모리 : " + f  + "bytes");
  System.out.println("");
 }
}

댓글 없음:

댓글 쓰기