-
Notifications
You must be signed in to change notification settings - Fork 0
/
gates.c
219 lines (148 loc) · 3.79 KB
/
gates.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "gates.h"
#include <stdio.h>
#include <assert.h>
/* Task 1 - Bit by Bit */
uint8_t get_bit(uint64_t nr, uint8_t i)
{
assert(i <= 8 * sizeof nr);
uint8_t res = -1;
/* TODO
*
* "res" should be 1 if the bit is active, else 0
*/
uint64_t mask = (1 << i);
if((nr & mask) != 0)
res = 1;
else
res = 0;
return res;
}
uint64_t flip_bit(uint64_t nr, uint8_t i)
{
assert(i <= 8 * sizeof nr);
uint64_t res = -1;
/* TODO
*
* Return the "nr" with the ith bit flipped
*/
uint64_t mask_set = ((uint64_t)1 << i);
res = nr ^ mask_set;
return res;
}
uint64_t activate_bit(uint64_t nr, uint8_t i)
{
assert(i <= 8 * sizeof nr);
uint64_t res = 0xFF;
/* TODO
*
* Return the "nr" with the ith bit "1"
*/
uint64_t mask = ((uint64_t)1 << i);
res = nr | mask ;
return res;
}
uint64_t clear_bit(uint64_t nr, uint8_t i)
{
assert(i <= 8 * sizeof nr);
uint64_t res = -1;
/* TODO
*
* Return the "nr" with the ith bit "0"
*/
uint64_t mask = ~((uint64_t)1 << i);
res = nr & mask ;
return res;
}
/* Task 2 - One Gate to Rule Them All */
uint8_t nand_gate(uint8_t a, uint8_t b)
{
assert (a == 0 || a == 1);
assert (b == 0 || b == 1);
return !(a & b);
}
uint8_t and_gate(uint8_t a, uint8_t b)
{
assert (a == 0 || a == 1);
assert (b == 0 || b == 1);
uint8_t res = -1;
/* TODO - Use the nand gate to implement the and gate */
uint8_t c = nand_gate(a,b);
res = nand_gate(c,c);
return res;
}
uint8_t not_gate(uint8_t a)
{
assert (a == 0 || a == 1);
uint8_t res = -1;
/* TODO - Use the nand gate to implement the not gate */
res = nand_gate(a,a);
return res;
}
uint8_t or_gate(uint8_t a, uint8_t b)
{
assert (a == 0 || a == 1);
assert (b == 0 || b == 1);
uint8_t res = -1;
/* TODO - Use the previously defined gates to implement the or gate */
uint8_t c1, c2;
c1 = nand_gate(a,a);
c2 = nand_gate(b,b);
res = nand_gate(c1,c2);
return res;
}
uint8_t xor_gate(uint8_t a, uint8_t b)
{
assert (a == 0 || a == 1);
assert (b == 0 || b == 1);
uint8_t res = -1;
/* TODO - Use the previously defined gates to implement the xor gate */
uint8_t c1, c2,c3;
c1 = nand_gate(a,b);
c2 = nand_gate(c1,a);
c3 = nand_gate(c1,b);
res = nand_gate(c2,c3);
return res;
}
/* Task 3 - Just Carry the Bit */
uint8_t full_adder(uint8_t a, uint8_t b, uint8_t c)
{
assert (a == 0 || a == 1);
assert (b == 0 || b == 1);
assert (c == 0 || c == 1);
uint8_t res = -1;
/* TODO - implement the full_adder using the previous gates
* Since the full_adder needs to provide 2 results, you should
* encode the sum bit and the carry bit in one byte - you can encode
* it in whatever way you like
*/
uint8_t d1, s, e1, e2, f;
d1 = xor_gate(a,b);
s = xor_gate(d1,c);
e1 = and_gate(d1,c);
e2 = and_gate(a,b);
f = or_gate(e1,e2);
/* octetul res va avea bitul dupa pozitia 0 cu valoarea lui s si bitul dupa pozitia 1 cu valoarea lui f = carry */
res = (f << 1) | s ;
return res;
}
uint64_t ripple_carry_adder(uint64_t a, uint64_t b)
{
uint64_t res = -1;
uint64_t j;
/* TODO
* Use the full_adder to implement the ripple carry adder
* If there is ANY overflow while adding "a" and "b" then the
* result should be 0
*/
uint8_t carry = 0;
res = 0 ;
for(j = 0; j <= 63; j++)
{
uint64_t m = full_adder((a >> j) & 1, (b >> j) & 1, carry);
res = res | ((m & (uint64_t)1) << j);
carry = (m >> (uint64_t)1) & (uint64_t)1 ;
}
if(carry == 1)
return 0;
return res ;
}