여러가지 연산자

Java 프로그래밍 - 여러가지 연산자_1

public class Main {
    public static void main(String[] args) {

//      1. 대입 연산, 부호 연산자
        int num = 100;
        num = 10;
        num = +10;
        num = -10;


//      2. 산술 연산자, 증가/감소 연산자
        System.out.println("== 산술 연산자, 증가/감소 연산자 ==");
        int numX = 10;
        int numY = 3;
        int result = 0;
        result = numX + numY;
        result = numX - numY;
        result = numX * numY;
        result = numX / numY;
        result = numX % numY;
        System.out.println(result);

        int numZ = 1;
        System.out.println(numZ++);
        System.out.println(numZ);

        numZ = 1;
        System.out.println(++numZ);
        System.out.println(numZ);

        numZ = 1;
        System.out.println(numZ--);
        System.out.println(numZ);

        numZ = 1;
        System.out.println(--numZ);
        System.out.println(numZ);


//      3. 관계 연산자
        System.out.println("== 관계 연산자 ==");
        int numA = 10;
        int numB = 9;

        System.out.println(numA > numB);
        System.out.println(numA < numB);
        System.out.println(numA == numB);
        System.out.println(numA != numB);


//      4. 논리 연산자
        System.out.println("== 논리 연산자 ==");
        System.out.println((10 > 9) && (1 == 0));
        System.out.println((10 > 9) || (1 == 0));


//      5. 복합 대입 연산자
        System.out.println("== 복합 대입 연산자 ==");
        int num1 = 10;
        int num2 = 5;
        num1 += num2;
        System.out.println(num1);
        num1 -= num2;
        System.out.println(num1);
        num1 *= 2;
        System.out.println(num1);
        num1 %= 3;
        System.out.println(num1);

//      6. 삼항 연산자
        System.out.println("== 삼항 연산자 ==");
        int a = 100;
        String aResult = (a == 100)? "yes" : "no";
        System.out.println("aResult = " + aResult);

    }

}
== 산술 연산자, 증가/감소 연산자 ==
1
1
2
2
2
1
0
0
0
== 관계 연산자 ==
true
false
false
true
== 논리 연산자 ==
false
true
== 복합 대입 연산자 ==
15
10
20
2
== 삼항 연산자 ==
aResult = yes

Java 프로그래밍 - 여러가지 연산자_2

public class Main {
    public static void main(String[] args) {

//      1. 비트 논리 연산자
        System.out.println("== 비트 논리 연산자 ==");
//      1-1. AND 연산자 (&)
        int num1 = 5;
        int num2 = 3;
        int result = 0;

        result = num1 & num2;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num1)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num2)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result)));

//      1-2. OR 연산자 (|)
        num1 = 5;
        num2 = 3;
        result = num1 | num2;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num1)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num2)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result)));

//      1-3. XOR 연산자 (^)
        num1 = 5;
        num2 = 3;
        result = num1 ^ num2;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num1)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num2)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result)));

//      1-4. 반전 연산자 (~)
        num1 = 5;
        result = ~num1;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(num1)));
        System.out.printf("%s\n", (Integer.toBinaryString(result)));


//      2. 비트 이동 연산자
        System.out.println("== 비트 이동 연산자 ==");
//      2-1. << 연산자
        int numA = 3;
        result = numA << 1;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(numA)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result)));

//      2-2. >> 연산자
        numA = 3;
        result = numA >> 1;
        System.out.println(result);
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(numA)));
        System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result)));

//      2-3. >>> 연산자
        numA = -5;
        result = numA >> 1;
        System.out.println(result);
        System.out.printf("%s\n", (Integer.toBinaryString(numA)));
        System.out.printf("%s\n", (Integer.toBinaryString(result)));

        numA = -5;
        result = numA >>> 1;
        System.out.println(result);
        System.out.printf("%s\n", (Integer.toBinaryString(numA)));
        System.out.printf("%s\n", (Integer.toBinaryString(result)));

    }

}
== 비트 논리 연산자 ==
1
0101
0011
0001
7
0101
0011
0111
6
0101
0011
0110
-6
0101
11111111111111111111111111111010
== 비트 이동 연산자 ==
6
0011
0110
1
0011
0001
-3
11111111111111111111111111111011
11111111111111111111111111111101
2147483645
11111111111111111111111111111011
1111111111111111111111111111101

Tags:

Categories:

Updated:

Leave a comment