Commit c28a9717d5e1e2aa9fa49f7d86cc05da85aa7c47
1 parent
824bccd3
--no commit message
Showing
4 changed files
with
47 additions
and
8 deletions
arith/Makefile
1 | -TARGETS = adder substractor multiplier divider negator comparator shifter | 1 | +TARGETS = adder substractor multiplier divider negator comparator shifter bit_adder |
2 | 2 | ||
3 | .PHONY: all clean | 3 | .PHONY: all clean |
4 | 4 | ||
@@ -20,4 +20,6 @@ negator: | @@ -20,4 +20,6 @@ negator: | ||
20 | comparator: | 20 | comparator: |
21 | gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator | 21 | gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator |
22 | shifter: | 22 | shifter: |
23 | - gcc -I../common/ ../common/auxiliar.c shifter.c -o shifter | ||
24 | \ No newline at end of file | 23 | \ No newline at end of file |
24 | + gcc -I../common/ ../common/auxiliar.c shifter.c -o shifter | ||
25 | +bit_adder: | ||
26 | + gcc -I../common/ ../common/auxiliar.c bit_adder.c -o bit_adder | ||
25 | \ No newline at end of file | 27 | \ No newline at end of file |
arith/bit_adder.c
@@ -2,8 +2,30 @@ | @@ -2,8 +2,30 @@ | ||
2 | #include <string.h> | 2 | #include <string.h> |
3 | #include "auxiliar.h" | 3 | #include "auxiliar.h" |
4 | 4 | ||
5 | +int check_length(char **inputs, uint8_t num_inputs) | ||
6 | +{ | ||
7 | + int input_length = strlen(inputs[0]); | ||
8 | + int i; | ||
9 | + for(i = 1; i < num_inputs; i++) | ||
10 | + { | ||
11 | + if(input_length != strlen(inputs[i])) | ||
12 | + { | ||
13 | + return -1; | ||
14 | + } | ||
15 | + } | ||
16 | + int max_value = 1; | ||
17 | + int max_bits = num_inputs * input_length; | ||
18 | + i = 0; | ||
19 | + while(max_value <= max_bits) | ||
20 | + { | ||
21 | + max_value = 0x1 << ++i; | ||
22 | + } | ||
23 | + return i; | ||
24 | +} | ||
25 | + | ||
5 | int main(int argc, char **argv) | 26 | int main(int argc, char **argv) |
6 | { | 27 | { |
28 | + int i; | ||
7 | if(argc != 2) | 29 | if(argc != 2) |
8 | { | 30 | { |
9 | error(); | 31 | error(); |
@@ -19,14 +41,13 @@ int main(int argc, char **argv) | @@ -19,14 +41,13 @@ int main(int argc, char **argv) | ||
19 | error(); | 41 | error(); |
20 | } | 42 | } |
21 | int num_inputs = num_occurrences(input,','); | 43 | int num_inputs = num_occurrences(input,','); |
22 | - if(num_inputs != 1) | 44 | + int input_length = check_length(inputs,num_inputs); |
45 | + int count = 0; | ||
46 | + for(i = 0; i < num_inputs; i++) | ||
23 | { | 47 | { |
24 | - error(); | 48 | + count += count_bits(inputs[i]); |
25 | } | 49 | } |
26 | - int input_length = strlen(inputs[0]); | ||
27 | - int num = bin2dec(inputs[0]); | ||
28 | - num *= -1; | ||
29 | - char *result = dec2bin(num,input_length); | 50 | + char *result = dec2bin(count,input_length); |
30 | printf("%s",result); | 51 | printf("%s",result); |
31 | free(result); | 52 | free(result); |
32 | free_mem(inputs,num_inputs); | 53 | free_mem(inputs,num_inputs); |
common/auxiliar.c
@@ -234,4 +234,19 @@ void reverse_two_complement(char *bin) | @@ -234,4 +234,19 @@ void reverse_two_complement(char *bin) | ||
234 | char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin)); | 234 | char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin)); |
235 | strcpy(bin,bin_added); | 235 | strcpy(bin,bin_added); |
236 | free(bin_added); | 236 | free(bin_added); |
237 | +} | ||
238 | + | ||
239 | +int count_bits(char *input) | ||
240 | +{ | ||
241 | + int i; | ||
242 | + int count = 0; | ||
243 | + int input_length = strlen(input); | ||
244 | + for(i = 0; i < input_length; i++) | ||
245 | + { | ||
246 | + if(input[i] == '1') | ||
247 | + { | ||
248 | + count++; | ||
249 | + } | ||
250 | + } | ||
251 | + return count; | ||
237 | } | 252 | } |
238 | \ No newline at end of file | 253 | \ No newline at end of file |
common/auxiliar.h
@@ -19,3 +19,4 @@ int bin2udec(char *bin); | @@ -19,3 +19,4 @@ int bin2udec(char *bin); | ||
19 | char* dec2bin(int dec, int length); | 19 | char* dec2bin(int dec, int length); |
20 | void expand_input(char **input, int required_length); | 20 | void expand_input(char **input, int required_length); |
21 | void reverse_two_complement(char *bin); | 21 | void reverse_two_complement(char *bin); |
22 | +int count_bits(char *input); | ||
22 | \ No newline at end of file | 23 | \ No newline at end of file |