반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
Tags
- state
- JSX
- Props
- c++
- DP
- server
- MySQL
- count
- event
- priority_queue
- nodeJS
- map
- routes
- bit
- component
- Callback
- node.js
- route
- queue
- React
- Context
- array
- css
- leetcode
- axios
- 비트연산
- UE5
- Navigation
- BinaryTree
- treenode
Archives
- Today
- Total
우사미 코딩
[Bit Manipulation] 190. Reverse Bits 본문
반응형
https://leetcode.com/problems/reverse-bits/
Reverse Bits - LeetCode
Can you solve this real interview question? Reverse Bits - Reverse bits of a given 32 bits unsigned integer. Note: * Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed
leetcode.com
1. 개념정리

2. 코드
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t result = 0;
int i = 0;
while(i < 32){
result = result << 1; // or result <<= 1;
uint32_t curr = n % 2;
result += curr;
n = n >> 1; // or n >>= 1;
i++;
// cout << "result : " << bitset<32>(result) << endl;
// cout << "n : " << bitset<32>(n) << endl;
}
return result;
}
};
uint32_t를 cout으로 출력하면 2진수 값이 출력되므로
32비트 2진수 값으로 출력하고 싶으면 bitset<32>(n) 을 사용한다.
반응형
'Leetcode' 카테고리의 다른 글
| [Leetcode] 153. Find Minimum in Rotated Sorted Array - 주옥같은 binary search (0) | 2023.05.17 |
|---|---|
| [LeetCode] 202. Happy Number - 주옥같은 해피넘버 (0) | 2023.05.17 |
| [Bit Manipulation] 231. Power of Two (0) | 2023.04.18 |
| [Bit Manipulation] 191. Number of 1 Bits - n & (n-1) trick (0) | 2023.04.18 |
| 시간복잡도, 공간복잡도 (Time complexity, Space complexity) (0) | 2023.04.18 |
Comments