Вопрос

  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?

Это было полезно?

Решение

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.

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top