precompiler arithmatic ^ (power) operator

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have been searching all through freaks.net and google for anything on this with no luck.

I know that the + - * and / operators work in #define statements, I thought that the ^(power) operator worked also. Evidently I'm wrong

#define GOOBER (2^8)

evaluates to 10. Wouldn't be a big deal, but for actual use the 8 is replaced with a nother constant. I could calculate these everytime I adjust something, but I like having a tidy code that's easy to tweek.

My question: Is there a way to use the power operator in the precompiler to calculate constants?

Thanks

Ben

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

^ is the exclusive or operator. If you need powers of two then bit shifting works fine (1 << n) is 2 to n:th power.
/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Ben,
the ^ operator is allready used for exclusive-OR bit-operation.
Using it for power operator would be at least confusing and I think not possible because it would define a second meaning for the ^ operator.

In other compilers I have seen that a power() function is available. Have not seen (or needed) that in the WINAVR GCC package.

good luck. EAP

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bnemec wrote:
I have been searching all through freaks.net and google for anything on this with no luck.

You need to go back to your basic 'C' textbook.

Quote:
the ^(power) operator worked also. Evidently I'm wrong

Very wrong.

'^' is not the "power" operator in the 'C' programming language. Never has been!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bnemec wrote:
I know that the + - * and / operators work in #define statements

In fact, they don't actually do anything at all in #define "statements"

#define GOOBER ( 2^8 )

just makes the preprocessor replace every occurrence of the text "GOOBER" with the text "( 2^8 )" - the preprocessor does not actually do any arithmetic at all here!

(the compiler probably evaluates the expression at compile time, but not the preprocessor)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...