PoshCode Archive  Artifact [0fc6a69e7b]

Artifact 0fc6a69e7b25a8ed7bed900f2a90fd2ae02c3e434dabf9b367d4bb290ef3dbfe:

  • File Shift-Operators-Correct.ps1 — part of check-in [46b05e6335] at 2018-06-10 13:25:56 on branch trunk — 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. (user: Joel Bennett size: 1966)

# 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)
   }
}
}