Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 30, 2020 01:56
Show Gist options
  • Save Ch-sriram/930cd33f5ab4b6e83bbdb17a0a546ca0 to your computer and use it in GitHub Desktop.
Save Ch-sriram/930cd33f5ab4b6e83bbdb17a0a546ca0 to your computer and use it in GitHub Desktop.
Given an integer N, find its binary representation.
#include <iostream>
#include <string>
using namespace std;
#define ull uint64_t
string bin_rep(ull n) {
if (n == 0) return "";
string rem(1, (n&1) + '0'); // https://www.geeksforgeeks.org/how-to-convert-a-single-character-to-string-in-cpp/
return bin_rep(n>>=1) + rem;
}
int main() {
int t; cin >> t;
while(t--) {
ull n; cin >> n;
string ans = (n == 0) ? "0" : bin_rep(n);
cout << ans << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment