[Geeks for Geeks] Binary representation

Binary representation

Write a program to print Binary representation of a given number N.

  • Time : O(1)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution
{
public:
string getBinaryRep(int N)
{
string res = "";
for(long long i = 29; i >= 0; i--) {
res.push_back((bool)(N & (1<<i)) | 0b110000);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/21/PS/GeeksforGeeks/binary-representation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.