Define bit vs. bit masking

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
Is defining a bit in C, such as:

#define Output_1 PORTAbits.RA0

The same thing as masking the same bit:

#define Output_1mask 0b00000001

Will this function the same when using bitwise operators?

Output_1 & other vs. Output_1mask & other

Thanks.
 

MrChips

Joined Oct 2, 2009
34,807
My guess would be that they are not the same.

Bit masking clearly uses bitwise logical operators whereas PORTAbits.RA0 is a structure using unions.

How the actual code is compiled and implemented will depend on the compiler and the native MCU instruction set.
Some MCUs have direct bit addressing instructions. MCUs that cannot address individual bits will have to resort to bitwise logical operators using masks.
 

joeyd999

Joined Jun 6, 2011
6,279
In C, in most cases a #define is simply text substitution.

In your example, all occurrences of Output_1 will be replaced, prior to compilation, with the text 'PORTAbits.RA0'. All occurrences of Output_1mask will be replaces with the text '0b00000001'.

Now, tell me if the contruct:

Output_1 & other

makes sense.
 

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
I appreciate all the responses, and sorry for my delayed response. I believe I have a grasp on the differences now.

Best
 

Papabravo

Joined Feb 24, 2006
22,082
I appreciate all the responses, and sorry for my delayed response. I believe I have a grasp on the differences now.

Best
One of them is an address and the other is just a numerical constant.
The constant is used to do operation on a bit with bytewide operations.
The address is used by specific operations that work on bits.
 
Top