Language/JAVA
Chapter3. 연산자 .3 단항 연산자
구일일구
2022. 8. 2. 16:32
반응형
[SignOperatorExample.java] 부호 연산자
package july25_chap3;
public class clon_sec03_ex1 {
public static void main(String[] args) {
int x = -100;
int result1 = +x; //+(-100) = -100
int result2 = -x; //-(-100) = 100
System.out.println("result1= " + result1);
System.out.println("result2= " + result2);
short s = 100;
//short result3 = -s; -부호 붙이는 것도 연산이기 때문에 int 써야함
int result3 = -s; //-(100) = -100
System.out.println("result3= " + result3);
}
}
</>
result1= -100
result2= 100
result3= -100
[IncreaseDecreaseOperatorExample.java] 증감 연산자
package july25_chap3;
public class clon_sec03_ex2 {
public static void main(String[] args) {
int x = 10;
int y = 10;
int z;
System.out.println("-----------------------");
x++; //뒤에 ++면, 바로 값이 변하지 않음. 변수를 가져다 쓸 때 +1이 됨 = 10
++x; //바로 +1 = x는 11에서 +1 = 12
System.out.println("x= " + x);
System.out.println("-----------------------");
y--; //y = 10
--y; //y = 9 -1 = 8
System.out.println("y= " + y);
System.out.println("-----------------------");
z = x++; //z = 12
System.out.println("z= " + z); //z=12
System.out.println("x= " + x); //x =12+1 = 13
System.out.println("-----------------------");
z = ++x; //z=13+1
System.out.println("z= " + z); //z = 14
System.out.println("x= " + x); //x = 14
System.out.println("-----------------------");
z = ++x + y++; //z = (14+1) + 8 = 23
System.out.println("z= " + z); //z = 23
System.out.println("x= " + x); //x = 15
System.out.println("y= " + y); //y = 9
}
}
</>
-----------------------
x= 12
-----------------------
y= 8
-----------------------
z= 12
x= 13
-----------------------
z= 14
x= 14
-----------------------
z= 23
x= 15
y= 9
[DenyLogicOperatorExample.java] 논리 부정 연산자
package july25_chap3;
public class clon_sec03_ex3 {
public static void main(String[] args) {
boolean play = true;
System.out.println(play); //true
play = !play; //true의 반대
System.out.println(play);
play = !play; //false의 반대
System.out.println(play);
}
}
</>
true
false
true
[BitReverseOperatorExample.java] 비트 반전 연산자
package july25_chap3;
public class clon_sec03_ex4 {
public static void main(String[] args) {
int v1 = 10;
int v2 = ~v1;
int v3 = ~v1 + 1;
System.out.println(toBinaryString(v1) + " (십진수: " + v1 + ")");
System.out.println(toBinaryString(v2) + " (십진수: " + v2 + ")");
System.out.println(toBinaryString(v3) + " (십진수: " + v3 + ")");
System.out.println();
int v4 = -10;
int v5 = ~v4;
int v6 = ~v4 + 1;
System.out.println(toBinaryString(v4) + " (십진수: " + v4 + ")");
System.out.println(toBinaryString(v5) + " (십진수: " + v5 + ")");
System.out.println(toBinaryString(v6) + " (십진수: " + v6 + ")");
}
public static String toBinaryString(int value) {
String str = Integer.toBinaryString(value);
while(str.length() < 32) {
str = "0" + str;
}
return str;
}
}
//10진수를 2진수로 바꾸는 법
//int v1 = 10
//32비트 = 00000000 00000000 00000000 00000000 = 0
//32비트 = 00000000 00000000 00000000 00001010 = 10
</>
00000000000000000000000000001010 (십진수: 10)
11111111111111111111111111110101 (십진수: -11)
11111111111111111111111111110110 (십진수: -10)
11111111111111111111111111110110 (십진수: -10)
00000000000000000000000000001001 (십진수: 9)
00000000000000000000000000001010 (십진수: 10)
반응형