-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_binary.c
More file actions
49 lines (42 loc) · 727 Bytes
/
print_binary.c
File metadata and controls
49 lines (42 loc) · 727 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
42
43
44
45
46
47
48
49
#include "main.h"
/**
* print_binary -> prints an unsigned number.
* @args: list of arguments.
* @array: arrays.
* @f:....
* @w:....
* @p:....
* @s:....
*
* Return: binary numbers.
*/
int print_binary(va_list args, char array[], int f, int w, int p, int s)
{
unsigned int n, m, i, sum;
unsigned int array_[32];
int count;
UNUSED(array);
UNUSED(f);
UNUSED(w);
UNUSED(p);
UNUSED(s);
n = va_arg(args, unsigned int);
m = 2147483648;
array_[0] = n / m;
for (i = 1; i < 32; i++)
{
m /= 2;
array_[i] = (n / m) % 2;
}
for (i = 0, sum = 0, count = 0; i < 32; i++)
{
sum += array_[i];
if (sum || i <= 32)
{
char z = '0' + array_[i];
write(1, &z, 1);
count++;
}
}
return (count);
}