Help C++ declaration

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Help C++ declaration

 
0
  #1
Mar 22nd, 2007
Hi All...
I'v a piece of code kindly help me understanding its functionality

  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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help C++ declaration

 
0
  #2
Mar 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: Help C++ declaration

 
0
  #3
Mar 22nd, 2007
Actually that code was like this...

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help C++ declaration

 
0
  #4
Mar 22nd, 2007
Did you read the articles I gave links to.. ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: Help C++ declaration

 
0
  #5
Mar 22nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help C++ declaration

 
2
  #6
Mar 22nd, 2007
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:
  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:
  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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help C++ declaration

 
0
  #7
Mar 23rd, 2007
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. */
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: Help C++ declaration

 
0
  #8
Mar 26th, 2007
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help C++ declaration

 
0
  #9
Mar 26th, 2007
Yes
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Help C++ declaration

 
0
  #10
Mar 27th, 2007
Yes. Copy paste err..
So now I know you've understood for sure..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC