JAVA에서 십진수를 이진수로 바꾸는 몇가지 방법 입니다. 총메모리사용량과 수행시간도 같이 표시 했습니다.
1. 일반적인 방법
1. 일반적인 방법
//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();
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");
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();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
2. shift 시키는 방법
//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;
}
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();
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");
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();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
3. 재귀호출
//10진수를 이진수로...
class DecimalToBinary2 {
int i=63;
static int[] b = new int[64];
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;
}
}
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();
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");
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();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
System.out.println("남은 메모리 : " + f + "bytes");
System.out.println("");
}
}
오라클자바커뮤니티에서 설립한 개발자교육6년차 오엔제이프로그래밍 실무교육센터(오라클SQL,튜닝,힌트,자바프레임워크,안드로이드,아이폰,닷넷 실무개발강의)
댓글 없음:
댓글 쓰기