Domanda

  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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top