-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_print_binary.c
69 lines (59 loc) · 1.04 KB
/
_print_binary.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "main.h"
/**
* print_binary - prints a number in binary format
* @x: number representing unsigned int value to print in binary format
* Return: none
*/
void print_binary(unsigned int x)
{
int i = 0, j, binary_digits[32];
if (x == 0)
{
_putchar ('0');
return;
}
while (x > 0)
{
binary_digits[i++] = x % 2;
x /= 2;
}
for (j = i - 1; j >= 0; j--)
{
_putchar(binary_digits[j] + '0');
}
}
/**
* print_binary_custom - handle custom conversion specifiers
* @x: number representing unsigned int value to print in binary format
* @format: pointer to string containing binary number
* Return: number of binary numbers printed
*/
int print_binary_custom(const char *format, unsigned int x)
{
int count = 0;
while (*format)
{
if (*format == '%')
{
format++;
if (*format == 'b')
{
print_binary(x);
count += sizeof(unsigned int) * 8;
}
else
{
_putchar('%');
_putchar(*format);
count += 2;
}
}
else
{
_putchar(*format);
count++;
}
format++;
}
return (count);
}