Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Quick Bitwise Operator Question

Status
Not open for further replies.

ste2006

Advanced Member level 4
Joined
May 1, 2012
Messages
118
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
2,226
This may seem like a stupid question but i am having a tough day of it

If i put a line as follows:

Code:
if ((SysFlags & 0x02) != 0x00) // Reset Flag Activated

Will the SysFlags register be affected by the AND operation??

Thanks,
 

Is this a register that clears the flag as soon as you read it?

Alex
 

No, No Just a variable i declared
 

You mentioned SysFlags register and I thought it was a register.
Since it is a variable no problem , you just read the value , mask it with 0x2 and then compare it to 0 , no assignment is done back to the variable.
 

Yes Sorry, I have spent so many nights now trying to finish some code before Friday i am starting to imagine things :)
 

Code:
if ((SysFlags & 0x02) != 0x00) // Reset Flag Activated

Your code is correct, but I think this is a better way to write it:


Code C - [expand]
1
if (SysFlags & 0x02) // Reset Flag Activated



The opposite test is written like this:

Code C - [expand]
1
if (!(SysFlags & 0x02)) // Reset Flag not Activated



By using that style, you eliminate the possibility to write broken code like this:

Code C - [expand]
1
if ((SysFlags & 0x02) == 1) // Reset Flag Activated



A related thing is the handling of "false" and "true".
Some programmers use declarations like this:

#define FALSE 0
#define TRUE 1

You don't see that in the original C book by Kernighan & Ritchie, and for a good reason.
An expression is "true" in C if it is non-zero. It is impossible to define a macro TRUE to have the same meaning for assignments and tests..
FALSE is a little better, but not much.
The following lines are broken:

Code C - [expand]
1
2
3
if ((SysFlags & 0x02) == TRUE) // Reset Flag Activated
...
if ((SysFlags & 0x02) == !FALSE) // Reset Flag Activated



This will work, but I don't recommend it:

Code C - [expand]
1
2
3
if ((SysFlags & 0x02) == !TRUE) // Reset Flag not Activated
...
if ((SysFlags & 0x02) != FALSE) // Reset Flag Activated



So my recommendation is to avoid FALSE, TRUE and comparisons with boolean variables.
Just do if(variable) or if(!variable).
 
Ok great,

thanks for the improved code,

I feel the way i am assing flags in somewhat cumbersome but i dont know of a better way, If i use a whole variable it is a waste of memory hence why i am trying to use numerous flags in one variable,

Anyone any thoughts??

Thanks,
 


I have used bit fields and my general recommendation is to avoid it. It is an elegant way to do some things, but the big problem is that almost everything about bit-fields is implementation-dependent. That means that the code will probably be non-portable. As an example, if you try to use bitfields to define data packets that should be sent between different computer architectures, you are in big trouble. There are no guarantees that you can write code that will place the fields in the same positions with two different compilers.

To use bit masks is the safe way, but it can be better to use a separate variable for each flag if you have enough memory.
"char" or "unsigned char" will be the smallest for ANSI C.
Some C compilers for some architectures have single-bit variables, but only use that if you don't need portability.
 

You can define

#define FALSE 0
#define TRUE !FALSE

and be safe.

There is no meaning to write it like that, because it is identical to "#define TRUE 1".

It is impossible to define TRUE so it can handle both these tests:


Code C - [expand]
1
2
3
4
if ((SysFlags & 0x02) == TRUE) // Reset Flag Activated
...
if ((SysFlags & 0x04) == TRUE) // XYZ Flag Activated
...



Some programmers create bit-manipulating/testing macros to shift down the result, so it can be tested for '0' or '1', but there is a big risk that the compiler can't optimize away all the extra code, so it can hurt code size and speed.

This is the best way, IMHO:

Code C - [expand]
1
2
3
4
if (SysFlags & 0x02) // Reset Flag Activated
...
if (SysFlags & 0x04) // XYZ Flag Activated
...



One improvement is to give names to the bit masks:


Code C - [expand]
1
2
3
4
5
6
7
#define RESET_FLAG 0x02
#define XYZ_FLAG   0x04
 
if (SysFlags & RESET_FLAG) // Reset Flag Activated
...
if (SysFlags & XYZ_FLAG) // XYZ Flag Activated
...



it is also simple to set/clear several bits at the same time:

Code C - [expand]
1
2
3
4
// Clear both flags
SysFlags &= ~(RESET_FLAG | XYZ_FLAG);
// Set both flags
SysFlags |= (RESET_FLAG | XYZ_FLAG);

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top