Hi All...
I'v a piece of code kindly help me understanding its functionality

#define FLAG_CDR 0*01 
#define FLAG_MSR 0*01

unsigned Flags = AFE_REG_FLAG_CDR | AFE_REG_FLAG_MSR;


Flags &= FLAG_MSR; //  'turn off cdr flag'

how it works...

Recommended Answers

All 10 Replies

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).

Actually that code was like this...

#define FLAG_CDR 0x01 
#define FLAG_MSR 0x02

unsigned Flags = AFE_REG_FLAG_CDR | AFE_REG_FLAG_MSR;


Flags &= FLAG_MSR; //  'turn off cdr flag'
Flags &= FLAG_CDR; //  'turn off msr flag'

please tell me how the flags get off.

Did you read the articles I gave links to.. ?

yah I know the concepts of bitwise OR and AND but plz tell me that how the flags get off when we take AND of
0x01&0x02

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;
}
commented: Rep from ~s.o.s~, I like your way of teaching... :D +15
commented: nice one +1

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
*/

Thanks alot for the help.
I'v got the complete understanding of the topic under discussion but let me correct one thing that You quoted and that is:

...
so if the 4th bit is set, flag & 0x08 evaluates to non-zero
if 4th bit is set, flag & 0x08 evaluates to zero.

May I correct it as

so if the 4th bit is set, flag & 0x08 evaluates to non-zero
if 4th bit is [B]NOT [/B]set, flag & 0x08 evaluates to zero.

Do you agree?

Yes

Yes. Copy paste err..
So now I know you've understood for sure.. :)

Yah I'v got it clear.
many thanks for the help...;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.