Blame view

arith/bit_adder.c 1017 Bytes
Imanol-Mikel Barba Sabariego authored
1
2
3
4
#include <stdio.h>
#include <string.h>
#include "auxiliar.h"
Imanol-Mikel Barba Sabariego authored
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int check_length(char **inputs, uint8_t num_inputs)
{
	int input_length = strlen(inputs[0]);
	int i;
	for(i = 1; i < num_inputs; i++)
	{
		if(input_length != strlen(inputs[i]))
		{
			return -1;
		}
	}
	int max_value = 1;
	int max_bits = num_inputs * input_length;
    i = 0;
    while(max_value <= max_bits)
    {
    	max_value = 0x1 << ++i;
    }
    return i;
}
Imanol-Mikel Barba Sabariego authored
26
27
int main(int argc, char **argv)
{
Imanol-Mikel Barba Sabariego authored
28
	int i;
Imanol-Mikel Barba Sabariego authored
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	if(argc != 2)
	{
		error();
	}
	char *input = argv[1];
	if(!strcmp(input,"error"))
	{
		error();
	}
	char **inputs = comma_separate(input);
	if(inputs == NULL)
	{
		error();
	}
	int num_inputs = num_occurrences(input,',');
Imanol-Mikel Barba Sabariego authored
44
45
46
	int input_length = check_length(inputs,num_inputs);
	int count = 0;
	for(i = 0; i < num_inputs; i++)
Imanol-Mikel Barba Sabariego authored
47
	{
Imanol-Mikel Barba Sabariego authored
48
		count += count_bits(inputs[i]);
Imanol-Mikel Barba Sabariego authored
49
	}
Imanol-Mikel Barba Sabariego authored
50
	char *result = dec2bin(count,input_length);
Imanol-Mikel Barba Sabariego authored
51
52
53
54
55
	printf("%s",result);
	free(result);
	free_mem(inputs,num_inputs);
	return 0;
}