I feel you've got something mixed up in copying these 2 lines
#define FLAG_CDR 0*01
#define FLAG_MSR 0*01
they should probably look somethig like:
#define FLAG_CDR 0x01
#define FLAG_MSR 0x01
This defines both flags to be same.
Anyway, just lookup the articles on bitwise operators and this should be clear. Here is another one .
This code is just trying to set some part (bits) of the value of flags variable to 0 (turning it off).
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Did you read the articles I gave links to.. ?
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
It's hard to explain in precisely the given code. I need to know types and values of AFE_REG_FLAG_CDR and AFE_REG_FLAG_MSR. Also what're all these flags used for?
In general may be some example will help:
Say you have a class that reprents a file. An object of this would have many states. Say our object has following:
1. good - when this flag is 1 it means the object is good for usage.
2. eof - when this flag is 1 it means the file pointer is at the end of file.
3. bof - when this flag is 1 it means the file pointer is at the begenning of file.
4. error - when this flag is 1 it means that last operation performed (read/write/seek...) on file failed.
One of implementing this would be to use 4 booleans. Another is to use a single char variable (whose size say is 1 byte = 8-bits) and use 4 bits to represent these 4 flags (other 4 are ignored).
Say like this:
8 7 6 5 4 3 2 1
^ ^ ^ ^
|<-- ignored-->| good err eof bof
Say in our class we provide 8 functions:
get_good(),set_good()
get_bad(),set_bad()
get_eof(),set_eof()
get_bof(),set_bof()
to get/set the values of these 4 flags.
Now if we had booleans: our function would look like this:
bool my_class::get_good() { return m_good_bool ; }
bool my_class::set_good(bool new_val) { m_good_bool = new_val;}
With bit-wise flags we do it like this:
class xxx {
private:
char flag ;
public:
xxx() {flag = 0x00;}
bool get_good() ;
void set_good() ;
};
void xxx::set_good()
{
/*
0x08 = 00001000
0x00 = 00000000
0x00 | 0x08 = 00001000
and 1 | X = 1 (where X = 0 or 1)
thus irrespective of the previous value of
4th bit, it'll be set to 1.
given that 0 | X = X (where X = 0 or 1)
with this we ensure that no other flag is chagned.
*/
flag |= 0x08 ;
}
bool xxx::get_good()
{
/*
1 & X = X (where X = 0 or 1)
0 & X = 0 (where X = 0 or 1)
0x00 = 00000000
0x08 = 00001000
0x00 & 0x08 = 00001000
0x08 = 00001000
0x08 = 00001000
0x00 & 0x08 = 00001000
so if the 4th bit is set, flag & 0x08 evaluates to non-zero
if 4th bit is set, flag & 0x08 evaluates to zero.
*/
return flag & 0x08 ;
}
int main()
{
xxx o ;
cout << "before = " << o.get_good() << endl ;
o.set_good() ;
cout << "after = " << o.get_good() << endl ;
return 0;
}
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Correction for comment in get_good()
/*
1 & X = X (where X = 0 or 1)
0 & X = 0 (where X = 0 or 1)
0x00 = 00000000
0x08 = 00001000
0x00 & 0x08 = 00000000
0x08 = 00001000
0x08 = 00001000
0x08 & 0x08 = 00001000
so if the 4th bit is set, flag & 0x08 evaluates to non-zero
if 4th bit is set, flag & 0x08 evaluates to zero.
0 converted to boolean is false
non-zero converted to boolean is true
*/
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Yes. Copy paste err..
So now I know you've understood for sure.. :)
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75