Skip to content

Instantly share code, notes, and snippets.

@maartentamboer
Created September 6, 2014 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maartentamboer/71448636da7f5f568d08 to your computer and use it in GitHub Desktop.
Save maartentamboer/71448636da7f5f568d08 to your computer and use it in GitHub Desktop.
Decimal value to binary string
#include <iostream>
#include <string>
#include <stdint.h>
using namespace std;
int main(int argc, char **argv)
{
uint32_t Dec = 1234;
uint32_t Comp = 1 << 31;
string BinStr;
for(int i = 0; i<=31; i++)
{
if((Comp & Dec) != 0)
{
BinStr.append("1");
}
else
{
BinStr.append("0");
}
Comp >>= 1;
}
cout << BinStr << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment