Created
June 27, 2023 06:26
-
-
Save ialexpovad/acd9485abd9941e35bcc575c4d1ef615 to your computer and use it in GitHub Desktop.
Decimal to binary format on C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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