Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BITWISE

Status
Not open for further replies.

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.
 
Do you mean:
(0xc0 & 0x80) for (11000000 AND 10000000)
(0xc0 ^ 0x80) for (11000000 XOR 10000000)
(0xc0 | 0x80) for (11000000 OR 10000000)
?
 
Given two bit strings of length A and B, I need to find the bitwise AND,OR and XOR of these two strings.

Exemple:
Enter str1(in binary):10010101
Enter str2(in binary :11010

Answer:
10010101
00011010(add zeros to the left)
________

XXXXXXXX AND
XXXXXXXX OR
XXXXXXXX XOR

Appreciate any help or suggestion.
 
Being that they are strings you will want to convert them from binary to a numerical number. YOu can do this with strtol

str A[32],B[32];
char* ptr;
// read in A & B

int aVal = strtol(A,ptr,2);
int bVal = strtol(B,ptr,2);

cout<<&quot;BITWISE AND: &quot;<<aVal&bVal;
cout<<&quot;BITWISE OR: &quot;<<aVal|bVal;
cout<<&quot;BITWISE XOR: &quot;<<aVal^bVal;

I think this is what you were looking for

Matt
 
Well, with string u'll have to
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_LEN 20
#define SEPARATOR &quot;---------------------------------------------------&quot;

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(&quot;\nEnter First bit sequence :&quot;);
	gets(str1);
printf(&quot;\nEnter Secnod bt sequence :&quot;);
	gets(str2);

len1=strlen(str1);
len2=strlen(str2);

if(len1>len2) 	
	insertZero(str2,len1);	
else 
	insertZero(str1,len2);

len1=strlen(str1);

printf(&quot;What operation do u want? [AND=1, OR=2, XOR=3]&quot;);
gets(resStr); //reusing a variable...
	idx=atoi(resStr);

switch(idx)
	{
	case 1: 
			doAnd(resStr,str1,str2); 
			printf(&quot;\n\n%s\nAND\n%s\n%.*s\n%s\n\n&quot;,
					str1,
					str2,
					len1,
					SEPARATOR,
					resStr);
			break;
	case 2: doOr(resStr,str1,str2); 
			printf(&quot;\n\n%s\nOR\n%s\n%.*s\n%s&quot;,
					str1,
					str2,
					len1,
					SEPARATOR,
					resStr);
			break;
	case 3: doXor(resStr,str1,str2);
			printf(&quot;\n\n%s\nXOR\n%s\n%.*s\n%s&quot;,
					str1,
					str2,
					len1,
					SEPARATOR,
					resStr);
			break;*/
	default: printf(&quot;\nInvalid operation request! try again...&quot;);
	}


return 0;
}


Similarly, u'll have to implement the doXor() and doOr() codes... I hope u get the idea.

have fun coding...:)

Roy.
user.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top