Doubt over iteration with multiple parameters

1

This loop realizes Log_base_2 of an integer value, at the end the result is available in the variable i .

Why does a shift of the form valor >> 1 have no effect compared to valor = valor >> 1 which by the way was the way I managed to avoid an loop / p>

for(i = 0 ; valor != 1 ; valor >> 1, ++ i); //loop infinito
for(i = 0 ; valor != 1 ; valor = valor >> 1, ++ i); //execução normal
    
asked by anonymous 29.04.2017 / 04:30

1 answer

1

The first one is just calculating a number and throwing away, it does not save anywhere, so it has no effect anywhere.

++i takes effect because it is a compound operator. In fact it is the same as i = i + 1 .

In this specific case you can do valor >>= 1 , which is the same as valor = valor >> 1 . This is also a composite operator, as well as a i += 1 as well.

    
29.04.2017 / 04:51