answersLogoWhite

0


Best Answer

There is no built-in logical operator that can achieve this. Logical operators operate upon Boolean operands (true and false) and output a Boolean value. Converting a set to a Boolean value won't tell us which individual elements exist within that set and without that vital information we cannot represent the intersection of two such sets.

To represent the intersection of two sets we need to represent the "universe of elements" as a bitmap, where each bit represents exactly one element of the universe. If the universe has 10 elements then we need a bitmap of 10 bits:

1111111111 = universe of elements

Note that a bitmap is nothing more than a binary value.

We can then represent the sets, A and B, as subsets of this universe:

A = 1011011011

B = 0101101101

If we perform a logical AND (operator &&) these binary values would implicitly convert to Boolean values. Given that both values are non-zero, they will both convert to true and the result would also be true:

C = A && B; // e.g., C = true && true = true

This would only tell us that an intersection exists, it will not tell us what the intersection actually is.

However, if we perform a bitwise AND (as opposed to a logical AND), we can determine which individual bits are present in both sets:

C = A & B; // e.g., C = 1011011011 & 0101101101 = 0001001001

Here we can see that both sets contain the 1st, 4th and 7th elements of the universe (starting from the low-order bit, reading right-to-left).

The built-in logical AND operator can only accommodate as many bits as will fit in a word. If the machine supports 64-bit operations, then the universe of elements is limited to 64 elements at most. If we have a larger universe, we cannot use built-in operators and must provide a named function instead. First, let's define a variable length set of integers as an array:

struct set {

int* data; // pointer to an array of integers

size_t size; // length of the array

};

Now we can define an intersect function:

set intersect (set A, set B) {

set C;

C.size = A.size>B.size?A.size:B.size; // use the larger of the two set sizes

C.data = malloc (C.size * sizeof (int)); // allocate memory

C.size=0; // start at index 0

for (int i=0; i<A.size, ++i) {

for (int j=0; j<B.size, ++j) {

if (A.data[i] == B.data[j]) C.data[C.size++] = A.data[i];

}

}

C.data = realloc (C.size * sizeof(int)); // shrink the array (remove unused elements)

return C;

}

This is clearly less efficient than using the built-in operator &. However, we can possibly improve the efficiency by converting the sets to bitmaps and dividing those bitmaps into groups of bits that the built-in operator can accommodate efficiently (such as 32-bit groupings), however the conversion processes also have to be taken into account to determine whether the overall efficiency is improved. This is left as an exercise for the reader.

User Avatar

Wiki User

5y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

The bitwise AND operator (symbolised with '&' in C, C++ and Java).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: A logical operator representing the intersection of two sets?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the intersection set?

is the result after doing intersection on 2 or more sets. It contains the elements which are common to all the sets on which intersection were done.


Do the intersection of two sets always contains a smaller number of terms than the union of two sets?

No, because the intersection of two equivalent sets will have a union the same size as its intersection.


The intersection of two sets of elements is represented by the word?

the intersection of two sets of elements is represented by the word: a)or b)and c)up


What is intersection of the set?

You normally do not have an intersection of only one set. The intersection of a set with itself is the set itself - a statement that adds little value. The intersection of two sets is the set which contains elements that are in each of the two sets.


What is the difference between the assignment operator and the equals operator?

The quality operator and the assignment operator are binary operators; they have two operands, one on either side of the operator. The equality operator is a Boolean operator which compares the two operands, returning true if they have the same logical state, otherwise false. E.g., x==y returns true if x and y have the same logical state, otherwise false. The operator is commutative, such that x==y is the same as y==x. The assignment operator sets the value of the left operand to that of the right operand, such that they both have the same logical state. After assignment, the left operand is returned. E.g., x=y returns x while y=x returns y. After the assignment, x==y must be true.


What is the meaning of intersection of two sets?

The intersection of two sets S and T is the set of all elements that belong to both S and T.


Do sets always have an intersection that is not the empty set?

No, they do not.


What is the set of members that two or more sets have in common?

That is called the intersection of the sets.


What are operations of sets?

union of sets,intersection of sets,difference of sets,ordered pair,ordered n-touples,cartician product of setThe basic operations are union and intersection. The complement of the set is also a basic operation.


Is there any case when union and intersection are same in sets?

if we have set A and B consider A={1,2,3,4}and B={3,4,5,6} the union of these sets is A and B={1,2,3,4,5,6}and the intersection is{3,4} the union and the intersection are same only if A=B


What is Intersection of Sets?

You need two sets to have an intersection. If you have two sets, call them R and S, then their intersection is the set T that contains all elements of R that also belong to S OR all elements of S and also belong to R...it's the same thing.


Intersection and union of sets of real numbers?

Yes, they can be very useful mathematical sets.