Bit Shifting in PowerShell

I needed to dome some Bit Shifting in PowerShell but unfortunately PowerShell lacks operator for Bit Shifting. I searched the .NET Framework for anything that allows for bit shifting but was unable to find anything suitable.

I didn’t want to revert to C# so I implemented shift left and shift right functions in PowerShell.

The code isn’t really pretty and could probably be improved (comments/improvements are welcome!) but here goes (please note that I implemented for bit shifting a byte):

Leave a Reply

  1. Shifting left = multiply by two (inserts a 0 at the right end);
    Shifting right = divide by two (drops the rightmost bit, and inserts a 0 at the left end)

    (integer math, no decimals)

    (this is ofcourse independent of the dev language used)

  2. @Luc: shift right doesn’t insert 0 but inserts 0 or 1 depending on the MSB (shift left always inserts 0)