Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Created June 27, 2023 06:26
Show Gist options
  • Save ialexpovad/acd9485abd9941e35bcc575c4d1ef615 to your computer and use it in GitHub Desktop.
Save ialexpovad/acd9485abd9941e35bcc575c4d1ef615 to your computer and use it in GitHub Desktop.
Decimal to binary format on C.
#include <cmath>
inline void dectobin(int indec, int len) {
using namespace std;
long ndec = indec % (long)(pow(2.0, len)); // truncating to max length!
for (int i = len - 1; i >= 0; i--)
{
long pw = (long)pow(2.0, i);
if (ndec - pw >= 0) {
printf("1");
ndec -= pw;
}
else {
printf("0");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment