954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help C++ declaration

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

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

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
 

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.

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

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
 

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

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

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
 

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 <strong>NOT </strong>set, flag & 0x08 evaluates to zero.



Do you agree?

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

Yes

WaltP
Posting Sage w/ dash of thyme
Moderator
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
 

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

Masood Ali
Newbie Poster
19 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You