210325 Thu

Sunny·2021년 4월 3일
0

Today I Learned

목록 보기
19/88
post-thumbnail

1. 첫 번째 학습 내용: 고급 연산자 (Advanced Operators)

  • 모든 언어에서 숫자형 변수는 표현 할 수 있는 범위가 정해져 있음
    예를 들어 8비트 Int형은 -128 에서부터 127까지를 표현 할 수 있음
  • UInt란? an unsigned integer 부호 없는 정수 (양수만 표현함)
    UInt8은 0부터 255까지의 정수 데이터 범위 가짐

비트 연산자 (Bitwise Operators)

  • Bitwise NOT Operator → ~
import UIKit

let initialBits: UInt8 = 0b00001111 // 15
let invertedBits = ~initialBits // equals 11110000 -> 16 + 32 + 64 + 128 = 240
  • Bitwise AND Operator → &
let firstSixBits: UInt8 = 0b11111100 // 252
let lastSixBits: UInt8 = 0b00111111 // 63
let middleFourBits = firstSixBits & lastSixBits // equals 00111100 = 60
  • Bitwise OR Operator → |
let someBits: UInt8 = 0b10110010
let moreBits: UInt8 = 0b01011110
let comebinedbits = someBits | moreBits
  • Bitwise XOR Operator → ^
    두 비트가 다른 경우에 1, 같은 경우에 0
let firstBits: UInt8 = 0b00010100
let otherBits: UInt8 = 0b00000101
let outputBits = firstBits ^ otherBits

부호없는 Integer의 시프팅 (Shifting Behavior for Unsigned Integers)

Q. 시프트 연산을 하는 이유? 컴퓨터가 곱셈과 나눗셈을 하기 위해서!
→ 왼쪽으로 시프트 하면 곱하기 2씩 됨.
→ 그말인 즉슨, 오른쪽으로 시프트 하면 나누기 2씩 됨.

Unit 23.3 시프트 연산자 사용하기, 길벗IT전문서

let shiftBits: UInt8 = 3
shiftBits << 3 // 24

부호있는 Integer의 시프팅 (Shifting Behavior for Signed Integers)

  • 맨 앞쪽에 있는 비트가 부호 (양수인지 음수인지) 표현
  • 0일 때는 양수, 1일 때는 음수 의미

1은 마이너스 의미.
나머지 7비트는 124 가짐 (128 - 4)

음수 표현 하는 법

예를 들어 4을 -4로 표현하고 싶다면
1. 최상단 비트 1로 바꿔서 음수 표현 0000 0100 → 1000 0100
2. 그 외의 비트들을 반전시켜 1111 1011으로 바꿔줌
3. 위 결과값에 1을 더해서 1111 1100으로 만듦

var positiveNumberFour = 4
var negativeNumberFour = ~positiveNumberFour + 1 // 비트들 반전시키고 1 더하기 -> -4
var positiveNumberFourReturned = ~negativeNumberFour + 1 // 다시 4로 바꿔보기

오버플로우 연산자 (Overflow Operators)

정해진 범위보다 더 큰 값을 넣으려고 한다면 잘못된 값이 들어가는 경우가 있음. 만약 값이 넘치는 오버플로우가 발생하더라도 그 뒷 값을 자르고 연산을 수행하려면 다음과 같이 각 연산자 앞에 &를 붙여 수행하면 됨.

  • 오버플로우 덧셈 (&+)
  • 오버플로우 뺄셈 (&-)
  • 오버플로우 곱셈 (&*)

var unsignedOverflow = UInt8.max
// unsignedOverflow equals 255, which is the maximum value a UInt8 can hold
unsignedOverflow = unsignedOverflow &+ 1
// unsignedOverflow is now equal to 0

똑같은 상황에서 오버플로우가 허용되지 않았다면, 값의 한도를 넘었기 때문에 에러가 날 것.

오버플로우 연산자를 사용함으로써 에러가 나지 않고 최대값인 255에서 1을 더했을 때 0이 되는 상황이 된다.

Int8은 최소값으로 -128 (2진수에서는 10000000)을 가질 수 있음.

이진수 10000000에서 1을 빼버리면 결과적으로 01111111가 되는데, 이는 부호 비트를 0으로 바꾸고 대신에 양수 127(Int8이 가질 수 있는 최대 양수)를 준다.

The minimum value that an Int8 can hold is -128, or 10000000 in binary. Subtracting 1 from this binary number with the overflow operator gives a binary value of 01111111, which toggles the sign bit and gives positive 127, the maximum positive value that an Int8 can hold.

For both signed and unsigned integers, overflow in the positive direction wraps around from the maximum valid integer value back to the minimum, and overflow in the negative direction wraps around from the minimum value to the maximum.

뭔가 시계처럼 돌고 돈다.

최대값 255에서 양수 방향으로 오버플로우 되면 최소값 1이 됨

최소값 -128에서 음수 방향으로 오버플로우 되면 최대값 127이 됨

C 언어 코딩 도장 - Unit 7.2 오버플로우와 언더플로우 알아보기

2. 두 번째 학습 내용: SOLID

SOLID란? 객체 지향 프로그래밍을 위한 5가지 원칙! (by 로버트 .C 마틴)

자료 출처

profile
iOS Developer

0개의 댓글