Go Down

Topic: isolating a digit (Read 1 time) previous topic - next topic

Paul Beaudet

thanks for for the explanation marco, I got that far, I was just not sure about
value=1106
value%=10
its this compound operator im not sure of, what is value equal to now?

I just tried a method comparing the ints a strings using itoa()...
that didn't go well  :smiley-eek: ... not even worth posting
itoa() seems to overflow when given an unsigned int with an unsigned'esk' value
and when blank spots in the buffer are compared they are random.

MichaelMeissner


thanks for for the explanation marco, I got that far, I was just not sure about
value=1106
value%=10
its this compound operator im not sure of, what is value equal to now?

The expression:

Code: [Select]

value %= 10;


is shorthand for:

Code: [Select]

value = value % 10;


The '%' operator is the modulus operator, which is equivalent to:

Code: [Select]

value = value - ((value / 10) * 10);


Note, integer division truncates, so if value was 1106 before the calculation, after 'value %= 10' it would hold 6.

So lets break it down:
1106 / 10 returns 110 (value / 10).
110 * 10 returns 1100 ((value / 10) * 10).
1106 - 1100 returns 6 (value - ((value / 10) * 10) or (value % 10).

spatula



thanks for for the explanation marco, I got that far, I was just not sure about
value=1106
value%=10
its this compound operator im not sure of, what is value equal to now?

The expression:

Code: [Select]

value %= 10;


is shorthand for:

Code: [Select]

value = value % 10;



@Paul, and this shorthand notation applies to other binary operators as well: +=, -=, *=, /=.
Where applicable, a op= b; is equivalent to a = a op b.

Thus, value = 1106 followed by value %= 10 will give you value = 6.
Now, allow me to expand a little bit: if value = 1106, what is value %= 1000

marco_c

#8
Apr 04, 2013, 08:06 am Last Edit: Apr 04, 2013, 08:09 am by marco_c Reason: 1
Quote
@Paul, and this shorthand notation applies to other binary operators as well: +=, -=, *=, /=.
Where applicable, a op= b; is equivalent to a = a op b.


Yes it does. It also applies to the logical operators |, &, ^, etc.

What's more, i++ is short for i+=1 which is short for i=i+1 :), but this form is only valud for ++ and --, not the other operators.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

PeterH


I just trusted the person knew what they were doing


Do you know what they're trying to achieve? Unless they're trying to do something really weird, there may well be a simpler way to achieve it.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up