GyeongHoKim / GyeongHoKim/algorithm

비트연산자로 소수점 내림하는 법

Open
#35 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C++
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Description

# 사건의 발단

배민 코테가 js로만 본다고 해서 열심히 js로 알고리즘 문제를 푸는 도중, js는 integer division operator가 없다는 사실을 깨닫고 파이썬의 round()같은 함수를 찾기 위해 구글에 js integer divide라고 검색하니 아래와 같은 글이 나왔습니다.

# 문제의 글

https://stackoverflow.com/questions/4228356/how-to-perform-an-integer-division-and-separately-get-the-remainder-in-javascr/4228528#4228528

저 글을 요약하자면 `~~(a/b)`를 하면 나머지를 버릴 수 있다, 이는 `(a/b) >> 0` 연산과 동일하다.
라는 것입니다.

# 궁금한점

`~`라는 연산자는 비트를 전부다 뒤집는 연산 아니겠습니까?
`~~a`를 하면 다시 원래의 수로 되돌아와야하는것이 순리인데 왜 이런 결과가 나오는 것일까요?
`>> 0`도 0번 shift한 것이니 아무것도 변하지 않아야 정상아니겟읍니까? 왜 저럴까요?

# 추가

https://stackoverflow.com/questions/1723575/how-to-perform-a-bitwise-operation-on-floating-point-numbers#:~:text=At%20the%20language%20level%2C%20there's,value%2Drepresentation%20of%20a%20number.&text=Floating%20point%20numbers%20don't,apply%20bitwise%20operations%20to%20them.

파이썬도 실험해보긴 했는데 C++도 마찬가지로 float형에 비트연산을 할 수 없다고 하는디 어떻게 된 일일까요??

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.