# encoding: ascii # api: powershell # title: Shift Operators(Correct) # description: Sorry, new to poshcode, I found Joels’ blog and wanted to point out that shifting by 0 bits is not supported by the function, its the pipelin logic handlign at fault really. # version: 0.1 # type: class # author: Joel Bennett # license: CC0 # function: Shift-Left # x-poshcode-id: 3453 # x-archived: 2012-06-18T09:49:24 # x-published: 2012-06-12T21:44:00 # # The missing Shift operators corrected so they shift in the right direction. # Note that for v1 you need to change Add-Type to New-Type # Shift-Left 16 1 returns 32 # Shift-Right 8 1 returns 4 # 8,16 |Shift-Left returns 16,32 # 2,4,8 |Shift-Right 2 returns 0,1,2 # #requires -version 2.0 Add-Type @" public class Shift { public static int Right(int x, int count) { return x >> count; } public static uint Right(uint x, int count) { return x >> count; } public static long Right(long x, int count) { return x >> count; } public static ulong Right(ulong x, int count) { return x >> count; } public static int Left(int x, int count) { return x << count; } public static uint Left(uint x, int count) { return x << count; } public static long Left(long x, int count) { return x << count; } public static ulong Left(ulong x, int count) { return x << count; } } "@ #.Example # Shift-Left 16 1 ## returns 32 #.Example # 8,16 |Shift-Left ## returns 16,32 function Shift-Left { PARAM( $x=1, $y ) BEGIN { if($y) { [Shift]::Left( $x, $y ) } } PROCESS { if($_){ [Shift]::Left($_, $x) } } } #.Example # Shift-Right 8 1 ## returns 4 #.Example # 2,4,8 |Shift-Right 2 ## returns 0,1,2 function Shift-Right { PARAM( $x=1, $y ) BEGIN { if($y) { [Shift]::Right( $x, $y ) } } PROCESS { if($_){ [Shift]::Right($_, $x) } } }