티스토리 뷰

학습내용

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 plus 3 equals 5
  • 5 remainder 4 equals 1
  • 1 times 5 equals 5

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

이럴수가... 😱 😭 왜 해줘...? 대체 왜...

 

[참고사이트]

 

문제점/고민한점 → 해결방안

  • 공식문서를 읽어보자...

'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
댓글