In: Computer Science
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i] = !a[i]; } } void func_1s_comp(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i] = !a[i]; } } void func_2s_comp(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i] = !a[i]; if(result[i] == '1' && carry == 1) { compTwo[i] = '0'; } else if(result[i] == '0' && carry == ddd1) { compTwo[i] = '1'; carry = 0; } else { compTwo[i] = result[i]; } compTwo[MAX] = '\0'; } } int main(void) { int choice; int carry = 1; int compTwo[]; int x[MAX]; int y[MAX]; int z[MAX]; do { printf("Enter the command number: \n0. Exit\n1. AND\n2. OR\n3. NOT\n4. 1's complement\n5. 2's complement\n6. 2's complement*\n"); scanf("%d", &choice); switch (choice) { case 0: //Exit break; case 1: //AND printf("Enter the first binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &x[i]); } printf("Enter the second binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &y[i]); } func_and(x, y, z); printf("The first number AND second binary yield: "); for (int i = 0; i < MAX; i = i + 1) { printf("%d", z[i]); } printf("\n"); case 2: //OR printf("Enter the first binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &x[i]); } printf("Enter the second binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &y[i]); } func_or(x, y, z); printf("The first number OR second binary yield: "); for (int i = 0; i < MAX; i = i + 1) { printf("%d", z[i]); } printf("\n"); case 3: //NOT printf("Enter the binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &x[i]); } func_not(x, z); printf("The NOT for this number is: "); for (int i = 0; i < MAX; i = i + 1) { printf("%d", z[i]); } printf("\n"); case 4: //1's complement printf("Enter the binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &x[i]); } func_1s_comp(x, z); printf("The 1st complement of this binary number is: "); for (int i = 0; i < MAX; i = i + 1) { printf("%d", z[i]); } printf("\n"); case 5://2's complement printf("Enter the binary number: \n"); for (int i = 0; i < MAX; i = i + 1) { scanf("%d", &x[i]); } func_2s_comp(x, z); printf("The 2nd complement of this binary number is: "); for (int i = 0; i < MAX; i = i + 1) { printf("%d", z[i]); } printf("\n"); case 6://2's complement* break; } } while (choice != 0); return 0; } I just have to add 2's complemenet and 2* complement which in the 2 functions which I dont know how to do. 2* complement is "Start from the right to left until you see digit 1, then pass it and NOT the digits after that. For instance, 2’s complement of 110100 is 001100"
/*First of all I think you have mistaken in declaring variables as comptwo[] is declared in main() but you used it in
func_2s_comp() it will show error so try declaring those variables globally or use similar variable in other functions.*/
/*I am going with the defination you provided for 2s* complement*/
//I am writing this complete sample code.
#include<stdio.h>
#define MAX 8
void main()
{ int i;
int a[MAX];
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
abc(a);
for(i=0;i<MAX;i++)
printf("%d",a[i]);
}
void abc(int bin[])//Function to calculate 2s *complement
{
int i,j;
//To get right most index where bit is 1
for(i=0;i<MAX;i++)
{
if(bin[MAX-1-i]==1)
{
i=MAX-1-i;
break;
}
}
//flipping all the bits to the left of rightmost 1
for(j=0;j<i;j++)
{
if(bin[j]==0)
bin[j]=1;
else
bin[j]=0;
}
}
void xyz(int bin[])//2s complement { int i,j; //To get 1s complement for(i=0;i<MAX;i++) { if(bin[i]==0) bin[i]=1; else bin[i]=0; } //To get right most index where bit is 0 for(i=0;i<MAX;i++) { if(bin[MAX-1-i]==0) { i=MAX-1-i; break; } } bin[i]=1;//flipping rightmost 0 //flipping all the 1s to the right of rightmost 0 for(j=i+1;j<MAX;j++) { bin[j]=0; } }
//OUTPUT
1. 2s * complement
2. 2s complement
//Thankyou