티스토리 뷰
학습내용
Uint8 / Uint16 / Uint32 / Int 의 자릿수
Bitwise Operators (비트연산자)
- NOT: (
~
) - AND: (
&
) - OR: (
|
) - XOR: (
^
) - Left Shift: (
<<
) - Right Shift: (
>>
)
Overflow Operators (오버플로우 연산자 == 값이 넘침)
- Overflow addition (
&+
) - Overflow subtraction (
&-
) - Overflow multiplication (
&*
)
Precedence
and Associativity (우선순위
와 결합성) - from 공식문서
Operator precedence gives some operators higher priority than others; these operators are applied first.
Operator associativity defines how operators of the same precedence are grouped together—either grouped from the left, or grouped from the right. Think of it as meaning “they associate with the expression to their left,” or “they associate with the expression to their right.”
It’s important to consider each operator’s precedence and associativity when working out the order in which a compound expression will be calculated. For example, operator precedence explains why the following expression equals 17
.
2 + 3 % 4 * 5
// this equals 17
If you read strictly from left to right, you might expect the expression to be calculated as follows:
2
plus3
equals5
5
remainder4
equals1
1
times5
equals5
However, the actual answer is 17
, not 5
. Higher-precedence operators are evaluated before lower-precedence ones. In Swift, as in C, the remainder operator (%
) and the multiplication operator (*
) have a higher precedence than the addition operator (+
). As a result, they’re both evaluated before the addition is considered.
아...???

print(2 + 3 % 4 * 5) // 17
print(5 + 3 * 2) // 11
print(5 * 3 + 2) // 17
print(15 & 12 ^ 10) // 6
이럴수가... 😱 😭 왜 해줘...? 대체 왜...

[참고사이트]
- Advanced Operators - the swift programming language(공식문서)
- [Swift] 고급 연산자 Part 1 - 비트연산자들1 Bitwise Operators1
- [Swift] 고급 연산자 Part 2 - 비트연산자들2 Bitwise Operators2 시프트연산 Shift Operators, 연산자 우선순위
- 컴퓨터가 10진수 숫자(정수)를 표현 하는 방법
- Swift 숫자를 문자로 변환 (2진수, 8진수, 16진수)
문제점/고민한점 → 해결방안
- 공식문서를 읽어보자...
'TIL' 카테고리의 다른 글
[공지] Kio는 블로그 이사 (0) | 2021.12.20 |
---|---|
TIL('21.03.29) - Unit Test / TDD (0) | 2021.03.30 |
TWL ('21.03.21~26) (0) | 2021.03.27 |
TIL ('21.03.25) (0) | 2021.03.26 |
TIL ('21.03.23) (0) | 2021.03.26 |
- Total
- Today
- Yesterday
- scope
- 삼항연산자
- overflow
- 객체지향 생활체조
- 범위연산자
- datatypes
- continue
- 훈련법
- 전산구문 기초용어
- 옵셔널
- swift
- conditional
- 산술연산자
- labeled
- Functions
- enumerations
- 반복문
- optional
- 비트연산자
- playground
- 세자리수마다 콤마넣기
- iOS View Life Cycle
- RawValues
- 구문이름표
- Constants
- variables
- 오버플로우연산자
- 오토레이아웃
- 흐름제어구문
- 결합성
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |