Lab #3 Bonus Assignment (16pts) Use bitwise operator to implement set operations
ID: 3871230 • Letter: L
Question
Lab #3 Bonus Assignment (16pts)
Use bitwise operator to implement set operations. For example, assume the universal set U = {a,b,c,d,e,f,g,h,i,j,k,l,m,n}. We can represent U in bits as follows: since |U| = 14, we can set U = 111111111111 (14 bits), meaning that a, b, …, m and n are in U. If A = {c,d}, then A = 00110000000000. The complement of A is {a,b,e,f,g,h,i,j,k,l,m,n}. Using XOR (exclusive or) with the binary representation for sets, the complement of A is 11001111111111 in binary.
Write a c++ program to prompt users to input two set A and B both of size 5. (Restrict the input letters to {a,b,c,d,…m,n}). Let U given above be the universal set.
a.Print out the binary representations for A and B.
b.Use bitwise operation to implement set union. Print out in the console the binary representation of A U B, and what bitwise operation you have used (i.e. and, or, xor , or a combination of and, or and xor.).
c.Set intersection. Print-outs follow (b).
d.Set difference A-B. Print-outs follow (b).
Explanation / Answer
C++ Program:-
#include<iostream>
using namespace std;
int main()
{
char a[14],b[14];
int binaryA[14]={0},binaryB[14]={0},unionSet[14],intersect[14],difference[14];
int set,binary=97;
cout<<"Enter 5 values for set A (only a to n) : ";
for(int i=0;i<5;i++)
{
cin>>a[i];
if(a[i]>'n' || a[i] < 'a')
{
i--;
}
}
cout<<"Enter 5 values for set B (only a to n) : ";
for(int i=0;i<5;i++)
{
cin>>b[i];
if(b[i]> 'n' || b[i] < 'a')
{
i--;
}
}
for(int i=0;i<14;i++)
{
for(int j=0;j<5;j++)
{
binary=97;
for(int k=0;k<14;k++)
{
set=0;
if(a[i]==(int)binary)
{
set=1;
}
if(set==1)
{
binaryA[k]=1;
break;
}
binary++;
}
}
}
binary=97;
for(int i=0;i<14;i++)
{
for(int j=0;j<5;j++)
{
binary=97;
for(int k=0;k<14;k++)
{
set=0;
if(b[i]==(int)binary)
{
set=1;
}
if(set==1)
{
binaryB[k]=1;
break;
}
binary++;
}
}
}
cout<<"The Binary Representation of set A : "<<endl;
for(int i=0;i<14;i++)
{
cout<<binaryA[i]<<" ";
}
cout<<endl;
cout<<"The Binary Representation of set B : "<<endl;
for(int i=0;i<14;i++)
{
cout<<binaryB[i]<<" ";
}
cout<<endl;
for(int i=0;i<14;i++)
{
unionSet[i]=binaryA[i] | binaryB[i];
intersect[i]=binaryA[i] & binaryB[i];
difference[i]=binaryA[i] - binaryB[i];
}
cout<<endl;
cout<<"The Union of set A,B"<<endl;
for(int i=0;i<14;i++)
{
cout<<unionSet[i]<<" ";
}
cout<<endl;
cout<<"The Intersection of set A,B"<<endl;
for(int i=0;i<14;i++)
{
cout<<intersect[i]<<" ";
}
cout<<endl;
cout<<"The Difference of set A,B"<<endl;
for(int i=0;i<14;i++)
{
cout<<difference[i]<<" ";
}
return 0;
}
OUTPUT
Enter 5 values for set A (only a to n) : a b c d f
Enter 5 values for set B (only a to n) : c d f g n
The Binary Representation of set A :
1 1 1 1 0 1 0 0 0 1 0 0 0 0
The Binary Representation of set B :
0 0 1 1 0 1 1 1 1 0 0 0 0 1
The Union of set A,B
1 1 1 1 0 1 1 1 1 1 0 0 0 1
The Intersection of set A,B
0 0 1 1 0 1 0 0 0 0 0 0 0 0
The Difference of set A,B
1 1 0 0 0 0 -1 -1 0 0 0 0 0 -1
Thank You!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.