-
Notifications
You must be signed in to change notification settings - Fork 3
/
print_bnr.c
42 lines (40 loc) · 876 Bytes
/
print_bnr.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
#include "main.h"
/**
* print_bnr - prints decimal in binary
* @arguments: input string
* @buf: buffer pointer
* @ibuf: index for buffer pointer
* Return: number of chars printed.
*/
int print_bnr(va_list arguments, char *buf, unsigned int ibuf)
{
int int_input, count, i, first_one, isnegative;
char *binary;
int_input = va_arg(arguments, int);
isnegative = 0;
if (int_input == 0)
{
ibuf = handl_buf(buf, '0', ibuf);
return (1);
}
if (int_input < 0)
{
int_input = (int_input * -1) - 1;
isnegative = 1;
}
binary = malloc(sizeof(char) * (32 + 1));
binary = fill_binary_array(binary, int_input, isnegative, 32);
first_one = 0;
for (count = i = 0; binary[i]; i++)
{
if (first_one == 0 && binary[i] == '1')
first_one = 1;
if (first_one == 1)
{
ibuf = handl_buf(buf, binary[i], ibuf);
count++;
}
}
free(binary);
return (count);
}