Frage

  1. When I execute j <<= 1 while j is initially zero, it ends up as a zero instead of 1 as it normally should be. Any other initial value is normally doubled.

    Does anyone know why this is happening?

  2. Why bitwise operations on slice fields cause runtime errors?

War es hilfreich?

Lösung

Consider j = 0:

0000 0000 0000 0000

Now what is j <<= 1? It's:

0000 0000 0000 0000
←                 ↑
Shifted         Added

Which is.. the same number (0).

Left shifting a number by 1, is multiplying it by 2, 0*2 is 0.

Andere Tipps

If j is zero, all of its bits are 0. When you shift the bits one position to the left, they all remain as zero, and a new zero bit is added on the right. Therefore all the bits in the result are zero, and the result is zero.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top