문제

  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