forked from dzabeligan/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_binary.c
More file actions
36 lines (31 loc) · 702 Bytes
/
print_binary.c
File metadata and controls
36 lines (31 loc) · 702 Bytes
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
#include "print_helpers.h"
/**
* print_binary_helper - Prints binary
* @n: int number to print
* @len: length of bytes written
*
*/
static void print_binary_helper(unsigned int n, int *len)
{
int last_digit = 0;
char hex[] = { '0', '1' };
last_digit = n & 0x1;
if (n > 0x1)
print_binary_helper(n >> 1, len);
*len += buffered_print(&hex[last_digit], 1);
}
/**
* print_binary - print binary
* @spec: specifier object
* @arg: pointer to arguments to be printed
*
* Return: length of bytes written
*/
int print_binary(specifier_t *spec, va_list arg)
{
int len = 0;
unsigned int num = va_arg(arg, unsigned int);
(void) spec;
print_binary_helper(num, &len);
return (len);
}