공부함/JAVA

[JAVA] Integer.toBinaryString : 10진수를 2진수로 변환하기

솔헬레나 2024. 2. 28. 16:44

[ 목적 ] 

10진수 -> 2진수로 변환하기

 

1)  Integer.toBinaryString

int i = 4;
String result = Integer.toBinaryString(i) ; 
system.out.print(result); // 결과 : 100 출력

 

 

2) 두개의 숫자를 2진수로 변환 하여 연산하기

//OR 연산
String orResult = Integer.toBinaryString(10 | 5);  // 10 : 1010 , 5 : 0101
System.out.println(orResult); // 결과 : 1111

// AND 연산
String andResult = Integer.toBinaryString(12 & 5); // 12 : 1100 , 6 : 0110
System.out.println(andResult); // 결과 : 100