티스토리 뷰
반응형
[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)
반응형
'Language > JAVA' 카테고리의 다른 글
열번째날 [인터페이스] (0) | 2022.08.04 |
---|---|
아홉째날 [상속 (2) 추상 클래스] (0) | 2022.08.03 |
Chapter2. 변수와타입 .3 타입변환 (0) | 2022.08.02 |
Chapter2. 변수와타입 .2 데이터타입 (0) | 2022.08.02 |
Chapter2. 변수와 타입 .1변수 (0) | 2022.08.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 프로그램
- java.time.package
- Arrays 클래스
- StringBuilder 클래스
- Format 클래스
- StringTokenizer 클래스
- 리스트연산자
- 포장 클래스
- 요소선택
- StringBuffer 클래스
- IndexError
- Random 클래스
- Date 클래스
- 자료형
- 순환할당
- FALSE
- 문자열함수
- 리스트
- 기본 API 클래스
- 딕셔너리
- not_in
- 함수
- Objects 클래스
- 역반복문
- Calendar 클래스
- Pattern 클래스
- 스레드 스케줄링
- 파이썬
- Math 클래스
- python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함