Old variable declaration

Hi:

I'm reading an old code that was programmed for Atmega32 but I don't really understand the following variable declaration

#define c_kinetic_pos 0x08
#define c_kinetic_neg 0x08
#define c_static_pos 0x0a
#define c_static_neg 0x0a

Can someone elaborate? , what do the 0x08 and 0x0a values mean?

This is not variable declaration but constant definition (see help for #define statement for details). It simply define an alias for hex value an compiler replace the text by this value whenever will be used in code.

Budvar10:
This is not variable declaration but constant definition (see help for #define statement for details). It simply define an alias for hex value an compiler replace the text by this value whenever will be used in code.

So c_kinetic_pos has a constant value of 8 and c_static_pos has a constant value of 10? if so, what's the advantage of using hex?

It is about making the code clear. It does not matter if is there used hex or dec value (or bin) - is the same.
The statement #define c_kinetic_pos 0x08 means that you can use c_kinetic_pos alias for value 0x08.
unsigned char i = c_kinetic_pos ; // there is 0x08 set in variable

Gilgamesh90:

Budvar10:
This is not variable declaration but constant definition (see help for #define statement for details). It simply define an alias for hex value an compiler replace the text by this value whenever will be used in code.

So c_kinetic_pos has a constant value of 8 and c_static_pos has a constant value of 10? if so, what's the advantage of using hex?

The advantage is that hex makes the bit patterns clearer than using decimal, so if the individual bits are important, like bits in a control register, it's a lot easier to read.
You could express them in binary, but it is a lot easier to lose your place when reading them, particularly for larger values.

AWOL:
The advantage is that hex makes the bit patterns clearer than using decimal, so if the individual bits are important, like bits in a control register, it's a lot easier to read.
You could express them in binary, but it is a lot easier to lose your place when reading them, particularly for larger values.

It would be even easier if we had eight fingers on each hand. :slight_smile:

Henry_Best:

AWOL:
The advantage is that hex makes the bit patterns clearer than using decimal, so if the individual bits are important, like bits in a control register, it’s a lot easier to read.
You could express them in binary, but it is a lot easier to lose your place when reading them, particularly for larger values.

It would be even easier if we had eight fingers on each hand. :slight_smile:

I suppose one could be annoying and state that ‘we have a total of 8 fingers across two hands’ (and two thumbs). 8) Granted that makes an incorrect assumption that the set “thumbs” isn’t a subset of the set “fingers”… :stuck_out_tongue:

Back to topic… Often what I do is for 8-bit values use binary notation (using the standard 0b prefix instead of the Arduino/Wiring B prefix), and larger I use hex notation and give the binary representation in a comment using octuplets. (I’m not one of the kool kids who can automatically see the bit representation of hex.)