Bit rotation question/challenge
Afgdr
zerzre.rertert at gmx.com
Sat Jan 30 14:40:49 UTC 2021
On Saturday, 30 January 2021 at 13:30:49 UTC, burt wrote:
> I have a static array of `ubyte`s of arbitrary size:
>
> ```d
> ubyte[4] x = [ // in reality, ubyte[64]
> 0b00001000,
> 0b00000001,
> 0b00010101,
> 0b11110010,
> ];
> ```
>
> Now I want to bit-rotate the array as if it is one big integer.
> So:
>
> ```d
> ubyte[n] rotateRight(size_t n)(ref const ubyte[n] array, uint
> rotation)
> {
> // ?
> }
> // same for rotateLeft
>
> ubyte[4] y = [
> 0b11111001,
> 0b00000100,
> 0b00000000,
> 0b10001010,
> ];
> assert(x.rotateRight(9) == y);
> assert(y.rotateLeft(9) == x);
> ```
>
> Any ideas how this could be achieved? I.e. what should go at
> the "?" for rotateRight and rotateLeft?
cast as uint and shift. cast the result as ubyte[4].
More information about the Digitalmars-d-learn
mailing list