Guest_imported
New member
- Jan 1, 1970
- 0
Given two bit strings of length A and B, I need to find the bitwise AND,OR and XOR of these two strings.
Appreciate any help or suggestion.
Appreciate any help or suggestion.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 20
#define SEPARATOR "---------------------------------------------------"
void insertZero(char *str, int ToLen)
{
int FrmLen=strlen(str);
int idx1, idx2;
for(idx1=FrmLen, idx2=ToLen;idx2>=0;idx1--,idx2--)
if(idx1>=0) str[idx2]=str[idx1];
else str[idx2]='0';
}
void doAnd(char *res, char *op1, char *op2)
{
int len=strlen(op1)-1, idx;
res[len+1]='\0';
for(idx=len;idx>=0;idx--)
if(op1[idx]=='1' && op2[idx]=='1')
res[idx]='1';
else
res[idx]='0';
}
main()
{
char str1[MAX_LEN], str2[MAX_LEN], resStr[MAX_LEN];
int idx, len1, len2;
printf("\nEnter First bit sequence :");
gets(str1);
printf("\nEnter Secnod bt sequence :");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1>len2)
insertZero(str2,len1);
else
insertZero(str1,len2);
len1=strlen(str1);
printf("What operation do u want? [AND=1, OR=2, XOR=3]");
gets(resStr); //reusing a variable...
idx=atoi(resStr);
switch(idx)
{
case 1:
doAnd(resStr,str1,str2);
printf("\n\n%s\nAND\n%s\n%.*s\n%s\n\n",
str1,
str2,
len1,
SEPARATOR,
resStr);
break;
case 2: doOr(resStr,str1,str2);
printf("\n\n%s\nOR\n%s\n%.*s\n%s",
str1,
str2,
len1,
SEPARATOR,
resStr);
break;
case 3: doXor(resStr,str1,str2);
printf("\n\n%s\nXOR\n%s\n%.*s\n%s",
str1,
str2,
len1,
SEPARATOR,
resStr);
break;*/
default: printf("\nInvalid operation request! try again...");
}
return 0;
}