What are bitwise shift (bit-shift) operators and how do they work?

后端 未结 11 1437
生来不讨喜
生来不讨喜 2020-11-21 04:46

I\'ve been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators) ...

What I\'m wondering

11条回答
  •  再見小時候
    2020-11-21 04:51

    Note that in the Java implementation, the number of bits to shift is mod'd by the size of the source.

    For example:

    (long) 4 >> 65
    

    equals 2. You might expect shifting the bits to the right 65 times would zero everything out, but it's actually the equivalent of:

    (long) 4 >> (65 % 64)
    

    This is true for <<, >>, and >>>. I have not tried it out in other languages.

提交回复
热议问题