-
Notifications
You must be signed in to change notification settings - Fork 0
/
S7b_Ex3-3.c
45 lines (40 loc) · 854 Bytes
/
S7b_Ex3-3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @file S7b_Ex3-3.c
* @author [email protected]
* @brief Convert integer to binary
* @note COMPILE gcc S7b_Ex3-3.c -g -Wall -Wextra -Wpedantic -o S7b33result
*/
#include <stdio.h>
#include <stdbool.h>
short getInput(void);
void numbToBinary(short number);
int main(void)
{
short number = getInput();
printf("The binary value is: ");
numbToBinary(number);
printf("\n");
return 0;
}
void numbToBinary(short number)
{
for (short i = 15; i >= 0; i--)
{
if (number & 1 << i)
printf("1");
else
printf("0");
}
}
short getInput(void)
{
bool saisieOK = false;
short number = 0;
printf("Convert to binary [0...65\'535]: ");
do
{
if (scanf("%hd", &number) == 1)
saisieOK = true;
} while (saisieOK != true);
return number;
}