-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_bin.c
More file actions
41 lines (39 loc) · 962 Bytes
/
printf_bin.c
File metadata and controls
41 lines (39 loc) · 962 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
37
38
39
40
41
#include "main.h"
/**
* printf_bin - prints a binary number.
* @val: argument to print.
*
* Return: number of binary digits printed.
*/
int printf_bin(va_list val)
{
unsigned int num;
/* To store binary representation of unsigned int in a 32-bit datum */
unsigned int bin_num[32];
int length, i, j;
/* Get unsigned int from variable argument list */
num = va_arg(val, unsigned int);
/* Length of binary digits printed */
length = 0;
/* If num equal zero, print a single '0' character and count 1 */
if (num == 0)
{
length++;
_putchar('0');
}
/**
* If num > 0, store the remainder of the division by 2 in the array,
* and increment length for each binary digit.
*/
for (i = 0; num > 0; i++)
{
bin_num[i] = num % 2;
num /= 2;
length++;
}
/* Printing the binary representation in reverse order */
for (j = i - 1; j >= 0; j--)
_putchar(bin_num[j] + 48);
/* Return the count of binary digits printed */
return (length);
}