본문 바로가기
JAVA/코드업

코드업 기초 100제 자바 [기초-비트단위논리연산, 삼항연산] 1059~1064

by 비전공자 기록광 2021. 7. 14.
반응형

https://codeup.kr/problemsetsol.php?psid=23 

 

문제집 / C언어 기초 100제

 

codeup.kr

 

1059 : [기초-비트단위논리연산] 비트단위로 NOT 하여 출력하기(설명)

입력 된 정수를 비트단위로 참/거짓을 바꾼 후 정수로 출력해보자.
비트단위(bitwise)연산자 ~ 를 붙이면 된다.(~ : tilde, 틸드라고 읽는다.)


** 비트단위(bitwise) 연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();
        
        System.out.printf("%d",~s);
    }
 
}
cs

 

 

1060 : [기초-비트단위논리연산] 비트단위로 AND 하여 출력하기(설명)

입력된 정수 두 개를 비트단위로 and 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise)연산자 &를 사용하면 된다.(and, ampersand, 앰퍼센드라고 읽는다.)


** 비트단위(bitwise)연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        
        String [] data = s.split(" ");
        
        int a = Integer.valueOf(data[0]);
        int b = Integer.valueOf(data[1]);
        
        System.out.println(a&b);
    }
 
}
cs

 

 

1061 : [기초-비트단위논리연산] 비트단위로 OR 하여 출력하기(설명) 

입력된 정수 두 개를 비트단위로 or 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise) 연산자 |(or, vertical bar, 버티컬바)를 사용하면 된다.


** | 은 파이프(pipe)연산자라고도 불리는 경우가 있다.

** 비트단위(bitwise) 연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        
        String [] data = s.split(" ");
        
        int a = Integer.valueOf(data[0]);
        int b = Integer.valueOf(data[1]);
        
        System.out.println(a|b);
    }
 
}
cs

 

 

1062 : [기초-비트단위논리연산] 비트단위로 XOR 하여 출력하기(설명)

입력된 정수 두 개를 비트단위로 xor 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise) 연산자 ^(xor, circumflex/caret, 서컴플렉스/카릿)를 사용하면 된다.

** 주의 ^은 수학식에서 거듭제곱(power)을 나타내는 기호와 모양은 같지만,
C언어에서는 전혀 다른 배타적 논리합(xor, 서로 다를 때 1)의 의미를 가진다.

** 비트단위(bitwise) 연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        
        String [] data = s.split(" ");
        
        int a = Integer.valueOf(data[0]);
        int b = Integer.valueOf(data[1]);
        
        System.out.println(a^b);
    }
 
}
cs

 

 

1063 : [기초-삼항연산] 두 정수 입력받아 큰 수 출력하기(설명)

입력된 두 정수 a, b 중 큰 값을 출력하는 프로그램을 작성해보자.
단, 조건문을 사용하지 않고 3항 연산자 ? 를 사용한다.

참고
3개의 요소로 이루어지는 3항(ternary) 연산자는
"조건식 ? (참일 때의 값) : (거짓일 때의 값)” 의 형태로 사용하는 연산자이다.
- 조건식의 계산 결과가 참인 경우에는 ':' 왼쪽의 값 또는 식으로 바뀌고,
- 거짓인 경우에는 ':' 오른쪽의 값 또는 식으로 바뀐다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        
        String[] data = s.split(" ");
        
        int a = Integer.valueOf(data[0]);
        int b = Integer.valueOf(data[1]);
        
        
        System.out.println(a>b?a:b);
        
    }
 
}
 
cs

 

 

 

1064 : [기초-삼항연산] 정수 3개 입력받아 가장 작은 수 출력하기(설명)

입력된 세 정수 a, b, c 중 가장 작은 값을 출력하는 프로그램을 작성해보자.
단, 조건문을 사용하지 않고 3항 연산자 ? 를 사용한다.

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        
        String[] data = s.split(" ");
        
        int a = Integer.valueOf(data[0]);
        int b = Integer.valueOf(data[1]);
        int c = Integer.valueOf(data[2]);
        
        int x = (a<b?a:b)<c?(a<b?a:b):c;
        
        System.out.println(x);
        
    }
 
}
cs

 

 

 

 

반응형

댓글