프로그래밍 언어/JAVA

[JAVA] 단항 연산자

김곰댕 2021. 6. 8. 15:02
728x90

단항 연산자

피연산자가 1개인 연산자

종류 : 부호연산자(+,-) , 증감연산자(++,--), 부정연산자(!), 비트 반전 연산자(~)


부호 연산자 : +, -

+ : 피연산자의 부호 유지

- : 피연산자의 부호 변경

boolean 타입과 char 타입을 제외한 기본 타입에 사용 가능

연산을 수행한 결과는 int 타입으로 결과가 산출됨

short s = 100;
short result = -s; //컴파일 에러 ( int 타입으로 결과가 산출되기 때문)
short s = 100;
int result3 = -s; //에러 일어나지 않음

예제)

package sec03.exam01_sign;

public class SingOperationExample 
{

    public static void main(String[] args) 
    {
        int x = -100;
        int result = +x;
        int result2 =-x;

        System.out.println(result);
        System.out.println(result2);

        short s = 100;
        //short result3 = -s;
        int result3 = -s;

        System.out.println(result3);
    }
}

출력 결과


증감 연산자 : ++, --

연산식 설명
++ 피연산자 다른 연산을 수행하기 전에 피연산자의 값을 1 증가 시킴
-- 피연산자 다른 연산을 수행하기 전에 피연산자의 값을 1 감소 시킴
피연산자 ++ 다른 연산을 수행한 후에 피연산자의 값을 1 증가 시킴
피연산자 --  다른 연산을 수행한 후에 피연산자의 값을 1 감소 시킴

예제)

package sec03.exam02_increase_decrease;

public class IncreaseDecreaseOperatorExample 
{
	public static void main(String[] args) 
	{
		int x = 10;
		int y = 10;
		int z;
		
		System.out.println("----------------------");
		x++; //x는 11
		++x; //x는 12
		System.out.println("x="+x);

		System.out.println("----------------------");
		y--; //y는 9
		--y; //y는 8
		System.out.println("y="+y);
		
		System.out.println("----------------------");
		z = x++; //x가 z에 대입 된 후 1 더해짐
		System.out.println("z="+z);
		System.out.println("x="+x);
		
		System.out.println("----------------------");
		z = ++x; //x가 1 더해진 후 z에 대입됨
		System.out.println("z="+z);
		System.out.println("x="+x);
		
		System.out.println("----------------------");
		z = ++x + y++; //x+1의 값과 y의값이 더해진 후 z에 대입, 그리고 y값 1증가
		
		System.out.println("x="+x);
		System.out.println("y="+y);
		System.out.println("z="+z);
	}
}

출력 결과


논리 부정 연산자 : !

! 피연산자 : 피연산자가 true이면 false 값을 산출 / 피연산자가 false이면 true 값을 산출

예제)

package sec03.exam03_deny_logic;

public class DenyLogicOperatorExample 
{
	public static void main(String[] args) 
	{
		boolean play = true;
		System.out.println(play);
		
		play = !play;
		System.out.println(play);
		
		play = !play;
		System.out.println(play);
	}
}

출력 결과


비트 반전 연산자 : ~

byte, short, int, long 타입만 피연산자가 될 수 있다.

비트값을 반전(0->1, 1->0) 시킨다.

부호 비트인 최상위 비트까지 반전되므로 부호가 반대인 새로운 값이 산출된다.

피연산자의 타입이 int 이하이면 연산의 결과는 int 타입이다.

비트 반전 후 1을 더하면 부호가 반대인 값을 얻을 수 있다.

 

정수를 비트의 값(2진수) 문자열로 출력하여 볼수있도록 하는 함수 : Integer.toBinaryString();

예제)

package sec03.exam04_bit_reverse;

public class BitReverseOperatorExample 
{
	public static void main(String[] args) 
	{
		int v1 = 10;
		int v2 = ~v1; 
		int v3 = ~v1 + 1; //v1과 부호가 반대인 값이 됨
		System.out.println(toBinaryString(v1) + "(십진수:" + v1 + ")"); //v1이 2진 문자열로 출력
		System.out.println(toBinaryString(v2) + "(십진수:" + v2 + ")"); //v2가 2진 문자열로 출력
		System.out.println(toBinaryString(v3) + "(십진수:" + v3 + ")"); //v3가 2진 문자열로 출력
		
		int v4 = -10;
		int v5 = ~v4;
		int v6 = ~v4 + 1; //v4와 부호가 반대인 값이 됨
		System.out.println(toBinaryString(v4) + "(십진수:" + v4 + ")"); //v4가 2진 문자열로 출력
		System.out.println(toBinaryString(v5) + "(십진수:" + v5 + ")"); //v5가 2진 문자열로 출력
		System.out.println(toBinaryString(v6) + "(십진수:" + v6 + ")"); //v6가 2진 문자열로 출력
	}
	
	public static String toBinaryString(int value)
	{
		String str = Integer.toBinaryString(value);
		while(str.length()<32) //str의 길이가 32보다 작으면 계속 반복
		{
			str = "0" + str;
		}
		return str; //32보다 크면 str을 리턴
	}
}

출력 결과

 

728x90