Today I’ve observed the rather peculiar behaviour where logical bitshifts on WordN
wrap around after N
bitshifts. Consider the following code & output:
let b : Word8 = 0xFF;
for (i in Iter.range(0, 10)) {
let k : Word8 = Word8.fromNat(i);
Debug.print(debug_show(b) # " >> " # debug_show(k) # " = " # debug_show(b >> k));
};
0xFF >> 0x0 = 0xFF
0xFF >> 0x1 = 0x7F
0xFF >> 0x2 = 0x3F
0xFF >> 0x3 = 0x1F
0xFF >> 0x4 = 0xF
0xFF >> 0x5 = 0x7
0xFF >> 0x6 = 0x3
0xFF >> 0x7 = 0x1
0xFF >> 0x8 = 0xFF
0xFF >> 0x9 = 0x7F
0xFF >> 0xA = 0x3F
This does not align with my understanding of logical bitshifts, nor do other languages (eg Go) seem to implement logical shifts this way.
I was unable to find any hint on that in the Motoko documentation. Is this behaviour on purpose? If so, what is the reason for implementing shifts this way?