I was given the assignment to create a program that performs basic set operations. the instructions are as follows:
You should represent sets as unsigned integer numbers, where each bit
of an unsigned number represents one possible element of a set. Thus,
we have 32 possible set elements (0, 1, … 31) and 32 bit positions in
an unsigned int variable (also 0, 1, … 31). For purposes of this
program it doesn’t matter whether you “name” the lowest order bit of
an unsigned int as 0 (and the highest order bit as 31) or if you
“name” the highest order bit 0 and lowest 31. In either case we will
define a ‘1’ in bit position i to mean that element i is a member of
the set represented by that unsigned int.
My first thought was to use an array to store the data however my professor told me not to do that and to just use unsigned ints. My question is how to go about implementing this? I was under the impression that if I was to set an unsigned int to 00000000000000000 when I print it out I would still get 0 when I output it. How am I supposed to check for each member of the set without having 4 million test cases?
He said for example to repesent a set {19, 3, 31, 12, 7} I would have
10000000000010000001000010001000
the declarations he provided that I will be required to build my definitions from are:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
extern unsigned int setUnion(unsigned int set1, unsigned int set2);
extern unsigned int setIntersection(unsigned int set1, unsigned int set2);
extern void clearSet(unsigned int *set);
extern void add2Set(unsigned int *set, int value);
extern void deleteFromSet(unsigned int *set, int value);
extern int isMember(unsigned int set, int element);
extern void printSet(unsigned int);
Any thoughts on how to do this would be much appreciated. I am not expecting yall to do my homework for me just kinda looking for some help on how to get started.
Use the C bitwise operators (
&,|,^,<<,>>and~) to access the individual bits value of yourunsigned intobjects.