| | |
Help C++ declaration
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi All...
I'v a piece of code kindly help me understanding its functionality
how it works...
I'v a piece of code kindly help me understanding its functionality
C++ Syntax (Toggle Plain Text)
#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'
I feel you've got something mixed up in copying these 2 lines
they should probably look somethig like:
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).
c Syntax (Toggle Plain Text)
#define FLAG_CDR 0*01 #define FLAG_MSR 0*01
c Syntax (Toggle Plain Text)
#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).
Last edited by thekashyap; Mar 22nd, 2007 at 4:04 am. Reason: Added the corrected version also.
Actually that code was like this...
please tell me how the flags get off.
C++ Syntax (Toggle Plain Text)
#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'
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:
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:
With bit-wise flags we do it like this:
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)
8 7 6 5 4 3 2 1 ^ ^ ^ ^ |<-- ignored-->| good err eof bof
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)
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:
C++ Syntax (Toggle Plain Text)
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; }
Correction for comment in get_good()
c Syntax (Toggle Plain Text)
/* 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:
May I correct it as
Do you agree?
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)
... so if the 4th bit is set, flag & 0x08 evaluates to non-zero if 4th bit is set, flag & 0x08 evaluates to zero.
so if the 4th bit is set, flag & 0x08 evaluates to non-zero
if 4th bit is NOT set, flag & 0x08 evaluates to zero.![]() |
Similar Threads
- String declaration in class definition (C)
- declaration syntax error? (C++)
- array declaration explanation needed (C)
- Array declaration problem (C++)
- DECLARATION SYNTAX ERROR (for bc 31 user) (C++)
- Parse error, syntax error, Forbids declaration (C++)
Other Threads in the C++ Forum
- Previous Thread: help with my for loop
- Next Thread: How to get cuurently Running App Name?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






