Near constant time rotate that does not violate the standards

后端 未结 4 1394
孤街浪徒
孤街浪徒 2020-11-27 20:05

I\'m having a heck of a time trying to come up with a constant time rotate that does not violate the C/C++ standards.

The problem is the edge/corner cases, where ope

4条回答
  •  猫巷女王i
    2020-11-27 20:43

    An alternative to the extra modulo is to multiply by 0 or 1 (thanks to !!):

    template  T rotlMod(T x, unsigned int y)
    {
        y %= sizeof(T) * 8;
        return T((x << y) | (x >> ((!!y) * (sizeof(T) * 8 - y)));
    }
    

提交回复
热议问题