943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2500
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 22nd, 2007
0

Help C++ declaration

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. #define FLAG_CDR 0*01
  3. #define FLAG_MSR 0*01
  4.  
  5. unsigned Flags = AFE_REG_FLAG_CDR | AFE_REG_FLAG_MSR;
  6.  
  7.  
  8. Flags &= FLAG_MSR; // 'turn off cdr flag'
how it works...
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 22nd, 2007
0

Re: Help C++ declaration

I feel you've got something mixed up in copying these 2 lines
  1. #define FLAG_CDR 0*01
  2. #define FLAG_MSR 0*01
they should probably look somethig like:
  1. #define FLAG_CDR 0x01
  2. #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).
Last edited by thekashyap; Mar 22nd, 2007 at 4:04 am. Reason: Added the corrected version also.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 22nd, 2007
0

Re: Help C++ declaration

Actually that code was like this...

C++ Syntax (Toggle Plain Text)
  1. #define FLAG_CDR 0x01
  2. #define FLAG_MSR 0x02
  3.  
  4. unsigned Flags = AFE_REG_FLAG_CDR | AFE_REG_FLAG_MSR;
  5.  
  6.  
  7. Flags &= FLAG_MSR; // 'turn off cdr flag'
  8. Flags &= FLAG_CDR; // 'turn off msr flag'
please tell me how the flags get off.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 22nd, 2007
0

Re: Help C++ declaration

Did you read the articles I gave links to.. ?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 22nd, 2007
0

Re: Help C++ declaration

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 22nd, 2007
2

Re: Help C++ declaration

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:
C++ Syntax (Toggle Plain Text)
  1. 8 7 6 5 4 3 2 1
  2. ^ ^ ^ ^
  3. |<-- 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:
C++ Syntax (Toggle Plain Text)
  1. bool my_class::get_good() { return m_good_bool ; }
  2. bool my_class::set_good(bool new_val) { m_good_bool = new_val;}

With bit-wise flags we do it like this:
C++ Syntax (Toggle Plain Text)
  1. class xxx {
  2. private:
  3. char flag ;
  4. public:
  5. xxx() {flag = 0x00;}
  6. bool get_good() ;
  7. void set_good() ;
  8. };
  9.  
  10. void xxx::set_good()
  11. {
  12. /*
  13.   0x08 = 00001000
  14.   0x00 = 00000000
  15.   0x00 | 0x08 = 00001000
  16.   and 1 | X = 1 (where X = 0 or 1)
  17.   thus irrespective of the previous value of
  18.   4th bit, it'll be set to 1.
  19.   given that 0 | X = X (where X = 0 or 1)
  20.   with this we ensure that no other flag is chagned.
  21.   */
  22. flag |= 0x08 ;
  23. }
  24.  
  25. bool xxx::get_good()
  26. {
  27. /*
  28.   1 & X = X (where X = 0 or 1)
  29.   0 & X = 0 (where X = 0 or 1)
  30.  
  31.   0x00 = 00000000
  32.   0x08 = 00001000
  33.   0x00 & 0x08 = 00001000
  34.  
  35.   0x08 = 00001000
  36.   0x08 = 00001000
  37.   0x00 & 0x08 = 00001000
  38.  
  39.   so if the 4th bit is set, flag & 0x08 evaluates to non-zero
  40.   if 4th bit is set, flag & 0x08 evaluates to zero.
  41.   */
  42. return flag & 0x08 ;
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48. xxx o ;
  49. cout << "before = " << o.get_good() << endl ;
  50. o.set_good() ;
  51. cout << "after = " << o.get_good() << endl ;
  52. return 0;
  53. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 23rd, 2007
0

Re: Help C++ declaration

Correction for comment in get_good()

  1. /*
  2.   1 & X = X (where X = 0 or 1)
  3.   0 & X = 0 (where X = 0 or 1)
  4.  
  5.   0x00 = 00000000
  6.   0x08 = 00001000
  7.  0x00 & 0x08 = 00000000
  8.  
  9.   0x08 = 00001000
  10.   0x08 = 00001000
  11.  0x08 & 0x08 = 00001000
  12.  
  13.   so if the 4th bit is set, flag & 0x08 evaluates to non-zero
  14.   if 4th bit is set, flag & 0x08 evaluates to zero.
  15.   0 converted to boolean is false
  16.   non-zero converted to boolean is true
  17. */
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 26th, 2007
0

Re: Help C++ declaration

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:

C++ Syntax (Toggle Plain Text)
  1. ...
  2. so if the 4th bit is set, flag & 0x08 evaluates to non-zero
  3. 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 NOT set, flag & 0x08 evaluates to zero.
Do you agree?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 26th, 2007
0

Re: Help C++ declaration

Yes
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Mar 27th, 2007
0

Re: Help C++ declaration

Yes. Copy paste err..
So now I know you've understood for sure..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with my for loop
Next Thread in C++ Forum Timeline: How to get cuurently Running App Name?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC