Fri, 22nd Oct 2021

package sec04.exam05_bit;

public class BitLogicExample {
	public static void main(String[] args) {
		System.out.println(45 & 25); //비트연산자 & : 두 비트 모두 1일 경우에 1을 출력
		//45 : 0010 1101
		//25 : 0001 1001
		//     0000 1001 -> 1*2^3 + 1*2^0 = 9
		System.out.println(45 | 25); //비트연산자 | : 두 비트 중 하나만 1일 경우 1을 출력
		//45 : 0010 1101
		//25 : 0001 1001
		//     0011 1101 -> 61 //계산기 사용하여 얻은 값
		System.out.println(45 ^ 25); //비트연산자 ^ : 두 비트 값이 서로 다를때만 1을 출력
		//45 : 0010 1101
		//25 : 0001 1001
		//     0011 0100 -> 52
		System.out.println(~45); //-46 //이유는 아래 참고
		System.out.println(toBinaryString(45)); //00000000000000000000000000101101
		System.out.println(toBinaryString(-46)); //11111111111111111111111111010010 //45의 보수
		System.out.println();
		
		System.out.println(toBinaryString(45));
		System.out.println("&");
		System.out.println(toBinaryString(25));
		System.out.println("||");
		System.out.println(45 & 25);
		
	}
	//toBinaryString() : 정수값을 총 32비트의 이진 문자열로 리턴하지만 앞의 비트가 모두 0이면 0은 생략하고 나머지 문자열만 리턴
	//32자리의 이진수를 얻기위해 아래와 같은 메소드 필요
	public static String toBinaryString(int value) {
		String str = Integer.toBinaryString(value);
		while(str.length() < 32) {
			str = "0" + str;
		}
		return str;
	}
	
}
9
61
52
-46
00000000000000000000000000101101
11111111111111111111111111010010

00000000000000000000000000101101
&
00000000000000000000000000011001
||
9
package sec04.exam05_bit;

public class BitShiftExample {
	public static void main(String[] args) {
		
		System.out.println(toBinaryString(1)); //처음에 적을 시 오류 발생하므로 아래에 메소드 선언 해주어야 함 //00000000000000000000000000000001
		System.out.println("<< 3"); //전체 비트를 앞으로 3자리씩 옮김
		System.out.println(toBinaryString(1<<3)); //00000000000000000000000000001000
		
		System.out.println(1<<3); //00000000000000000000000000001000 = 1*2^3 = 8
		System.out.println(-8>>3); //-1
		System.out.println(toBinaryString(-8>>3)); //11111111111111111111111111111111
		System.out.println(-8>>>3); //536870911
		System.out.println(toBinaryString(-8>>>3)); //00011111111111111111111111111111
		
	}

	public static String toBinaryString(int value) {
		String str = Integer.toBinaryString(value);
		while(str.length() < 32) {
			str = "0" + str;
		}
		return str;
	}
}
package sec04.exam06_assignment;

public class AssignmentOperatorExample {
	public static void main(String[] args) {
		int result = 0;
		
		result += 10; //result = result + 10 = 10
		System.out.println(result); //10
		
		result -= 5; //result = result - 5 = 5
		System.out.println(result); //5
		
		result *= 3; //result = result * 3 = 5 * 3 = 15
		System.out.println(result); //15
		
		result /= 5; //result = result / 5 = 15 / 5 = 3
		System.out.println(result);
		
		result %= 3; //result = result / 3 으로 나눈 나머지 = 3 / 3 = 몫은 1, 나머지 0
		System.out.println(result); //0
	}
}
package sec04.exam07_conditional;

public class ConditonalOperationExample {
	public static void main(String[] args) {
		int score = 85;
		
		char grade = (score > 90 ? 'A' : ((score > 80) ? 'B' : 'C'));
		//삼향연산자 : (조건) ? A : B -> 조건이 true이면 A, false이면 B
		//중첩해서 사용 가능 : (조건1) ? A : (조건2) ? B : C -> 조건1이 true면 A, false면 조건2 검사. 조건 2가 true면 B, false면 C
		System.out.println(grade); //B
	}
}

'Java > 3. 연산자 (Operator)' 카테고리의 다른 글

Day 4 : Exercise - Operator  (0) 2021.10.25
Day 3 : Practice #1  (0) 2021.10.25
Day 3 : 연산자 (Operator)  (0) 2021.10.25

+ Recent posts