View previous topic :: View next topic |
Author |
Message |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
Not zero constant |
Posted: Wed Nov 24, 2004 5:53 am |
|
|
Is there a way to define a not zero constant?
Something like:
#define CONST_NAME !0
Thank you _________________ Alex |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Nov 24, 2004 6:40 am |
|
|
Code: | #define CONST_NAME !0
#define CONST_NAME2 ~0
void main()
{
int8 a, b;
int16 c;
a = CONST_NAME; // a = 0x01
b = CONST_NAME2; // b = 0xFF
c = CONST_NAME2; // c = 0x00FF
} |
|
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Wed Nov 24, 2004 6:47 am |
|
|
ckielstra wrote: | Code: | #define CONST_NAME !0
#define CONST_NAME2 ~0
void main()
{
int8 a, b;
int16 c;
a = CONST_NAME; // a = 0x01
b = CONST_NAME2; // b = 0xFF
c = CONST_NAME2; // c = 0x00FF
} |
|
is the CONST_NAME always '1'?
what about all others except zero? _________________ Alex |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Nov 24, 2004 7:15 am |
|
|
Quote: | Is there a way to define a not zero constant?
|
Maybe you should explain how you want to use it so that it will be more clear to us what you are trying to do. |
|
|
Ttelmah Guest
|
Re: Not zero constant |
Posted: Wed Nov 24, 2004 10:49 am |
|
|
alexz wrote: | Is there a way to define a not zero constant?
Something like:
#define CONST_NAME !0
Thank you |
Firstly, what you show here, is a macro, not a 'constant'. A macro, is just an instruction to the compiler to substitute the right hand text, where the 'name' is given. As such, it has some really major 'caveats'. If (for instance), you define a macro as shown, then code:
X = CONST_NAME*2;
This will be converted to:
X = !0*2;
Now the result will depend on the arithmetic precedence. '*', fortunately has a lower precedence than the 'not' operator, so the result will be as expected, but it is the cause of many unexpected results when using macro expansions like this. Hence it is always safer to 'bracket' the right hand term, which forces the entire 'assembly' to have a higher precedence.
If you want a 'non zero constant', the normal method, would be:
const int8 non_zero = !0;
Which will store a constant 8 bit integer, containing the logical 'not' of '0'.
That macros are not 'constants', can easily be demonstrated, by having a second #define, and changing the value.
Best Wishes |
|
|
|