-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_bits.c
45 lines (41 loc) · 874 Bytes
/
print_bits.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
43
44
45
#include <unistd.h>
#include <stdio.h>
void print_bits(unsigned char octet)
{
char byte[9] = "00000000";
int n = octet;
int i = 0;
int relate = 128;
int rest = 0;
printf("n is: %i\n", n);
while(relate > n){
relate /= 2;
i ++;
}
byte[i++]= '1';
printf("relate is: %i\n", relate);
rest = relate / 2;
while(rest >= 2 && relate != n){
while((relate + rest) > n){
rest /=2;
printf("Rest is: %i\n", rest);
i++;
}
byte[i] = '1';
relate = relate + rest;
rest /= 2;
printf("relate is: %i\n", relate);
i++;
}
if (relate + rest == n)
byte[7] = '1';
i = 0;
while(byte[i])
write(1, &byte[i++], 1);
}
int main()
{
unsigned char octet = 222;
print_bits(octet);
return 0;
}