Skip to content

Instantly share code, notes, and snippets.

@zhrkvl
Created July 4, 2015 22:44
Show Gist options
  • Select an option

  • Save zhrkvl/950c47611872e78d49cf to your computer and use it in GitHub Desktop.

Select an option

Save zhrkvl/950c47611872e78d49cf to your computer and use it in GitHub Desktop.
Dec to bin
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <cstring>
#include <map>
#include <iomanip>
#include <queue>
using namespace std;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define maxl numeric_limits<int>::max()
#define maxll numeric_limits<long long>::max()
#pragma pack(push, 1)
struct bts
{
unsigned b0: 1;
unsigned b1: 1;
unsigned b2: 1;
unsigned b3: 1;
unsigned b4: 1;
unsigned b5: 1;
unsigned b6: 1;
unsigned b7: 1;
};
#pragma pack(pop)
union cl
{
int a;
bts bt[4];
char fl[4];
} cic;
string getbin(const int dec);
int main()
{
cout << getbin(12312) << endl;
return 0;
}
string getbin(const int dec) {
cl temp;
temp.a = abs(dec);
if(dec == 0)
return "00000000";
int bc = 3;
while(temp.fl[bc] == 0)
bc--;
string ans;
ans.reserve(32);
for(int i = bc; i >= 0; i--)
{
ans += temp.bt[i].b7 + '0';
ans += temp.bt[i].b6 + '0';
ans += temp.bt[i].b5 + '0';
ans += temp.bt[i].b4 + '0';
ans += temp.bt[i].b3 + '0';
ans += temp.bt[i].b2 + '0';
ans += temp.bt[i].b1 + '0';
ans += temp.bt[i].b0 + '0';
}
return ans;
}
@zhrkvl
Copy link
Copy Markdown
Author

zhrkvl commented Jul 4, 2015

Kek)00))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment