PoshCode Archive  Artifact [50ae380be7]

Artifact 50ae380be7e6d3558bdc4255f1e0c7662bf8e13522779274748939431acd33f6:

  • File Shift-Operators-Correct.ps1 — part of check-in [1c9d81d1c5] at 2018-06-10 14:26:04 on branch trunk — The missing Shift operators corrected so they shift in the right direction. (user: Joel Bennett size: 1750)

# encoding: ascii
# api: powershell
# title: Shift Operators(Correct)
# description: The missing Shift operators corrected so they shift in the right direction.
# version: 0.1
# type: class
# author: Joel Bennett
# license: CC0
# function: Shift-Left
# x-poshcode-id: 892
# x-archived: 2009-02-25T08:02:48
#
# 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)
   }
}
}