Can you demystify this macro?

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

From danni debouce code:

TCNT0 = (u8)(s16)-(XTAL / 1024 * 10e-3 + 0.5);   // preload for 10ms

XTAL is 1MHz. I understand that this takes the 8-bit timer counter and subtracts from zero (because this runs during the overflow interrupt) enough ticks so that it will overflow at about 10ms again.

The numbers math:

1000000/1024 * .01 + .05 = 10.266

I have no problems with that

(u8)(s16)

If you subtract from u8 you will "flip" back to 255 and go down from there, I get that.

My Question: What purpose does the s16 server in this macro?

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

Think about it. The pre-processor is calculating a negative real number. Casting to a signed int. Then truncating to an unsigned char.

Edit. The C compiler can and does Maths in doubles. The casts ensure that the C compiler only uses integer math with the result.

David.

Last Edited: Thu. Apr 10, 2008 - 05:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply david.

I was getting caught thinking (u8)(s16) was a multiplication function when it is not.

If I understand this correctly, the s16 allows for the function to evaluate to a negative, in this case, -10. The preprocessor then takes that -10 and converts it to an unsigned 8-bit number which is 10 less than zero, in this case, 246.

Do I have it now? If so, why is the s16 taken into consideration BEFORE the u8? (I have a feeling the answer to this is "because")

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

I do not know. Simplest thing is to remove one of the casts. I would guess that you get an unnecessary error message. Try it and see.

David.

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

Thanks, I'll give it a shot.

One more quick question... I don't think this warrants a new thread. Does this evaluate linearly:

  key_press |= key_state & i;

In other words, will the "or" function evaluate before the "and" function like this:

key_press = (key_press | key_state) & i;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At a guess, the & will be evaluated first. However it is a LOT safer to use parentheses and then you KNOW the order.

You can look it up yourself.

David.

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

Quote:
At a guess, the & will be evaluated first.

No need to guess, any assignment operator (including |=, &=, etc) has the lowest precedence (other than the comma), so will always be last.

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
Quote:
At a guess, the & will be evaluated first.

No need to guess, any assignment operator (including |=, &=, etc) has the lowest precedence (other than the comma), so will always be last.

Excellent, thanks for clarifying that for me.

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

Wikipedia: Operators in C and C++

Regards
Sebastian

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

david.prentice wrote:

> Before anyone jumps in. The pre-processor can and does Maths in
> doubles. The casts ensure that the C compiler only uses integer
> math with the result.

*jump in*

The /preprocessor/ can only do integer maths. However, the
preprocessor rarely performs any maths itself: it only does so in
order to evaluate the control expression of an #if statement.

The preprocessor only replaces strings here. It's actually the
compiler that performs the maths, and yes, the compiler /can/ do
floating-point calculations. As all the results of the calculations
are known at compile-time, no floating-point code will actually be
emitted.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Sorry Jorg, I should have differentiated between math in #if statements and the C compiler doing math on the final textual substitution.

I have edited the error in my original post.

David.