Suppose I have the following
(I've chagned the application from my company's domain (Healhtcare) to the good old days of being a student for proprietary reasons :) )
#define MATH 1
#define ECONOMICS 2
#define PSYCHOLOGY 4
#define SOCIOLOGY 8
which are possible values of a bitmap that contains course(s) of study at a university.
That is, with the STUDENT structure you'd have
char Majors; // a student can have 2 majors so for an econ/psych major, this would be 4+2=6
And I might have a local variable
char OneMajor;
I know that I can check if a student is taking major course OneMajor by writing:
if (student.Major & OneMajor)
printf("Matched!\n");
e.g. in the above example, if OneMajor = 4 (PSYCHOLOGY), and student.Major = 6, the IF would pass.
Now today I found the following IF in some undocumented code:
if ( (student.Major & OneMajor) == OneMajor)
printf("Mached!\n");
My questoin is, are these two IF's the same? I ran a few tests, and based on a VERY FEW cases,
it seems that if the"&" succeeds, the value of (student.Major & OneMajor) is Major.
If indeed that's correct, then can I safely say that
if (student.Major & OneMajor)
and
if ( (student.Major & OneMajor) == OneMajor)
will accomplish the same thing for me?
Thanks,
Steve
Start Free Trial