-
Notifications
You must be signed in to change notification settings - Fork 1
/
Carry_LookAhead_Adder.c
50 lines (47 loc) · 1.17 KB
/
Carry_LookAhead_Adder.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
#include<stdio.h>
#include<math.h>
int main(){
int n;
printf("Enter the bit-size: ");
scanf("%d",&n);
int A[n], B[n], a, b, cin, s[n], g[n], p[n], sum=0;
printf("Enter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);
for (int i=0;i<n;i++){
A[i]=a%2;
B[i]=b%2;
a=a/2;
b=b/2;
}
printf("Binary form of A: ");
for (int i=n-1;i>=0;i--){
printf("%d",A[i]);
}
printf("\nBinary form of B: ");
for (int i=n-1;i>=0;i--){
printf("%d",B[i]);
}
printf("\n Addition process");
cin=0;
for (int i=0; i<=n-1; i++){
printf("\n\nA[%d]=%d, B[%d]=%d",i,A[i],i,B[i]);
s[i]=(A[i]^B[i])^cin;
g[i]=A[i]&B[i];
p[i]=A[i]^B[i];
cin=g[i]|(p[i]&cin);
printf("\nS=%d, g=%d, p=%d, cin=%d\n_______________________",s[i],g[i],p[i],cin);
}
printf("\n\n SUM in binary: ");
for(int i=n-1;i>=0;i--)
{
printf("%d",s[i]);
sum+=s[i]*pow(2,i);
}
sum=sum+cin*pow(2,n);
printf("\n Carry :%d",cin);
printf("\n\n SUM in decimal: %d", sum);
getch();
return 0;
}