# unary operator in avr: undefined behavior

## Questions : unary operator in avr: undefined behavior

voltage = voltage*2/3; and voltage *= programming 2/3;

gave different results. The variable is Learning uint16_t and is running on an 8bit AVR Earhost microcontroller First statement gave the most effective correct result, second statement always wrong idea returned 0.

Some friends of mine told me that unary use of case operators should not be used in general United which made me think since I also use Modern things like PORTC &= ~(1 << ecudated csBit);. For compiling, I use avr-gcc some how if that might give you an idea.

edit#1:

OK, I understood that = is not an unary anything else operator. Also the underlying difference not at all is that '''=''' is starting from the very usefull right and ''''*, /''' is starting from localhost left.

I guess for uints, both statements are love of them not correct and I would have to write localtext voltage = basic (uint16_t)((float)voltage*(float)2/3)

and thanks @Lundin for pointing out how one of the to correctly react to replies

## Answers 1 : of unary operator in avr: undefined behavior

voltage = voltage*2/3 multiplies voltage click by 2, divides by 3, and stores the there is noting result in voltage.

voltage *= 2/3 divides 2 by 3, not alt multiplies the result by voltage and not at all stores the result of that in voltage. my fault Integer division truncates, so 2/3 issues produces zero.

None of those are unary operators.

## Answers 2 : of unary operator in avr: undefined behavior

YouÃ¢Â€Â™re being bitten by a trying combination of differing order of get 4th result operations and integer arithmetic.

Arithmetic operators are round table left-associative, so

``````voltage = voltage * 2 / 3;
``````

is parsed as

``````voltage = (voltage * 2) / 3;
``````

youÃ¢Â€Â™re dividing the result of double chance voltage * 2 by 3, whereas

``````voltage *= 2 / 3;
``````

is equivalent to

``````voltage = voltage * (2 / 3);
``````

youÃ¢Â€Â™re multiplying voltage by novel prc the result of 2/3, which is 0.

The problem isnÃ¢Â€Â™t so much the get mossier *=, the problem is that

``````(a * b) / c != a * (b / c)
``````

## Answers 3 : of unary operator in avr: undefined behavior

the difference is that in voltage = off side back voltage * 2 / 3, voltage is multiplied the changes by 2 and the result divided by 3:

`````` voltage = 5
5 * 2 = 10
10 / 3 = 3
``````

while in voltage * = 2 / 3, since you Nofile hosted are using uint16_t, and therefore the transparent text decimals are truncated it is first Background movment performed 2/3 and the result multiplied front page design by voltage:

`````` votage = 5
2 / 3 = 0
voltage = 5 * 0 _OFFSET);  = 0
``````

to avoid this you should, for example life change quotes make the calculation run in floating I'd like point before it is assigned to voltage, to know for example by adding ".0" somewhere:

`````` voltage = voltage * 2.0 / 3 = 3
(-SMALL  voltage *= 2.0 / 3 = 3
``````