-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvertToBin.c
More file actions
184 lines (158 loc) · 4.62 KB
/
Copy pathconvertToBin.c
File metadata and controls
184 lines (158 loc) · 4.62 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include <stdbool.h>
#define s 32
typedef struct{
int stk[s];
int tos;
} Stack;
//This function creates a new stack and starts the tos at -1
Stack newStack(){
Stack stack;
stack.tos = -1;
return stack;
}
int pop(Stack* stack){
int data = stack->stk[stack->tos];
stack->tos--;
return data;
}
void push(Stack* stack, int data){
stack->tos++;
stack->stk[stack->tos] = data;
}
bool isFull(int tos){
bool full = false;
if(tos == s -1)
full = true;
return full;
}
bool isEmpty(int tos){
bool empty = false;
if(tos == -1)
empty = true;
return empty;
}
int intValue(){
int val;
printf("Enter integer: ");
scanf("%d", &val);
return val;
}
void convertBin(int num){
int tNum, rem;
int binNum = 0;
bool empty, full;
Stack stack = newStack(); //Creates a new stack that has a tos == -1
tNum = num; // Temporary number that copied the value of number input.
/*
The while loop works because binary is always divisible by to.
ex. 1101 = 13
because
1101 = (8*1) + (4*1) + (2*0) + (1*1) = 13
With this reasoning what we can do to get binary is reverse the process.
instead of multiplying we divide.
Let say we get need to get the right most bit.
first we divide 13 by 1
res = 13/1
res = 13
then we modulo it by 2 because binary is always 2
bit = 13%2
bit = 1
Now we have our first Bit. We push this into the stack and loop.
Now the next equation is (2*0) So instead of dividing by 1 we divide by 2.
res = 13/2
res = 6
then we modulo it by 2 again
bit = 6%2
bit = 0
And now we have our second Bit 0. We push that into the stack and loop.
But now our next equation is (4*1) and now the left number is 4 not 2.
Since we already divided 2 by res we also need to divide 2 to 4.
So we divide res by 2 still
res = 6/2
res = 3
then we modulo it by 2 again
bit = 3%2
bit = 1
And we get the 3rd bit which is 1. We push that into the stack and loop
Our final equation is (8*1). You might think we divide 8 by 2 and get 4.
But now we didnt just divide 2 once but twice so we have to divide 8 by 2 twice or divide by 4 once.
8/4 is 2 so we divide res by 2.
res = 3/2
res = 1
then modulo it by 2 again
bit = 1%2
bit = 1
And we get our final Bit 1. We push that to the stack and loop.
Even though in our equation we are done the while loop is not done.
But since 1/2 is 0 and 0%2 is 0 the next bit would by 0 and 01101 = 1101
*/
while (tNum !=0) {
rem = tNum % 2;
full = isFull(stack.tos);
if(!full)
push(&stack, rem);
else{
printf("stack full\n");
return;
}
tNum /= 2;
}
empty = isEmpty(stack.tos);
/*
Now our stack is full of the bits we want to display.
But you might have realized that the numbers in the stack are reversed.
inside the stack its gonna look like [1,0,1,1] instead of [1,1,0,1].
So we can use a normal for loop normally.
There alot of ways to display the binary even if the array is reverse.
One is just reverse the for loop. But since we cant use the array and only pop.
What we can do is either print each time we pop the stack and not make a new line.
So the output is reversed.
What I did is pop the bit into binNum. and then multiplying it by 10.
So step by step it looks like.
//before while loop
binNum = 0
//1st loop
binNm = binNum * 10
binNum = {0} * 10
binNum = 0
binNum = binNum + pop(&stack)
binNum = {0} + {1}
binNum = 1
//2nd loop
binNm = binNum * 10
binNum = {1} * 10
binNum = 10
binNum = binNum + pop(&stack)
binNum = {10} + {1}
binNum = 11
//3nd loop
binNm = binNum * 10
binNum = {11} * 10
binNum = 110
binNum = binNum + pop(&stack)
binNum = {110} + {0}
binNum = 110
//3nd loop
binNm = binNum * 10
binNum = {110} * 10
binNum = 1100
binNum = binNum + pop(&stack)
binNum = {1100} + {1}
binNum = 1101
//Loop end
and now we have the binary converted from integer.
*/
while(!empty){
binNum *= 10;
binNum += pop(&stack);
empty = isEmpty(stack.tos);
}
printf("binary: %d\n", binNum);
}
int main(){
int num;
num = intValue();
convertBin(num);
return 0;
}