Skip to content

Instantly share code, notes, and snippets.

@zwazel
Created November 1, 2022 13:53
Show Gist options
  • Save zwazel/5a8de78acbd0973fef9df315cd29b191 to your computer and use it in GitHub Desktop.
Save zwazel/5a8de78acbd0973fef9df315cd29b191 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
void toBinary(int num, int pInt[5]);
int main() {
// endless loop counting the time in seconds
int second_10th = 0;
int second_1st = 0;
int minute_10th = 0;
int minute_1st = 0;
while (1) {
// turn the num into an array of binary digits
int binary_second_10th[5];
int binary_second_1st[5];
toBinary(second_10th, binary_second_10th);
toBinary(second_1st, binary_second_1st);
int binary_minute_10th[5];
int binary_minute_1st[5];
toBinary(minute_10th, binary_minute_10th);
toBinary(minute_1st, binary_minute_1st);
// print the binary array
printf("Time: %d%d:%d%d = ", minute_10th, minute_1st, second_10th, second_1st);
for (int j = 4; j >= 0; j--) {
printf("%d", binary_minute_10th[j]);
}
printf(" ");
for (int j = 4; j >= 0; j--) {
printf("%d", binary_minute_1st[j]);
}
printf(":");
for (int j = 4; j >= 0; j--) {
printf("%d", binary_second_10th[j]);
}
printf(" ");
for (int j = 4; j >= 0; j--) {
printf("%d", binary_second_1st[j]);
}
printf("\n");
// increment the seconds
second_1st++;
if (second_1st == 10) {
second_1st = 0;
second_10th++;
}
if (second_10th == 6) {
second_10th = 0;
minute_1st++;
}
if (minute_1st == 10) {
minute_1st = 0;
minute_10th++;
}
sleep(1);
}
return 0;
}
void toBinary(int num, int pInt[5]) {
int i = 0;
while (num > 0) {
pInt[i] = num % 2;
num /= 2;
i++;
}
while (i < 5) {
pInt[i] = 0;
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment