-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-print_binary.c
More file actions
48 lines (44 loc) · 804 Bytes
/
1-print_binary.c
File metadata and controls
48 lines (44 loc) · 804 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
#include "main.h"
/**
* _pow - func calculates (base ^ power)
* @base: base of the exponent
* @power: power of the exponent
*
* Return: value of (base ^ power)
*/
unsigned long int _pow(unsigned int base, unsigned int power)
{
unsigned long int num;
unsigned int a;
num = 1;
for (a = 1; a <= power; a++)
num *= base;
return (num);
}
/**
* print_binary - prints a number in binary notation
* @n: number to print
*
* Return: void
*/
void print_binary(unsigned long int n)
{
unsigned long int divisor, check;
char flag;
flag = 0;
divisor = _pow(2, sizeof(unsigned long int) * 8 - 1);
while (divisor != 0)
{
check = n & divisor;
if (check == divisor)
{
flag = 1;
_putchar('1');
}
else if (flag == 1 || divisor == 1)
{
_putchar('0');
}
divisor >>= 1;
}
}