How to do zero-fill right shift in Dart?

谁说我不能喝 提交于 2019-12-06 03:50:33

Zero-fill right shift requires a specific integer size. Since integers in Dart are of arbitrary precision the '>>>' operator doesn't make sense there.

The easiest way to emulate a zero-fill right shift is to bit-and the number first.

Example:

(foo & 0xFFFF) >> 2 // 16 bit zero-fill shift
(foo & 0xFFFFFFFF) >> 2 // 32 bit shift.

You could define a utility function to use:

int zeroFillRightShift(int n, int amount) {
  return (n & 0xffffffff) >> amount;
}

That assumes you have 32-bit unsigned integers and that's ok if you do have.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!