<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PowerShell needs Shift operators &#8230;</title>
	<atom:link href="http://huddledmasses.org/powershell-needs-shift-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org/powershell-needs-shift-operators/</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sun, 14 Mar 2010 05:17:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Klaus Schulte</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212303</link>
		<dc:creator>Klaus Schulte</dc:creator>
		<pubDate>Tue, 03 Mar 2009 15:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212303</guid>
		<description>Excellent!
I did already add a snapin ( My first SnapIn !!! ) to powershell with exactly that functionality but only using the type long int. That&#039;s a nice excercise ... but the &quot;Add-Type&quot; cmdlet makes these things SO MUCH easier ... that&#039;s brilliant!!!

I should go now and buy me the T-Shirt with the usual red heart, the &quot;PS&gt;_&quot; and the &quot;I love the PS team&quot;  on it!

best regards, Klaus</description>
		<content:encoded><![CDATA[<p>Excellent!<br />
I did already add a snapin ( My first SnapIn !!! ) to powershell with exactly that functionality but only using the type long int. That&#8217;s a nice excercise &#8230; but the &#8220;Add-Type&#8221; cmdlet makes these things SO <span class="caps">MUCH</span> easier &#8230; that&#8217;s brilliant!!!</p>
<p>I should go now and buy me the T-Shirt with the usual red heart, the &#8220;PS&gt;_&#8221; and the &#8220;I love the PS team&#8221;  on it!</p>
<p>best regards, Klaus</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel 'Jaykul' Bennett</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212268</link>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
		<pubDate>Sat, 28 Feb 2009 02:46:56 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212268</guid>
		<description>Wow, nice work :)

The speed is only a hair off the Add-Type one I wrote, as far as I can tell ... but yeah, that&#039;s about as complicated as things come.  I think either one would work, but yours is a lot easier to use on PowerShell v1, since you wouldn&#039;t need the &quot;New-Type script to stand in for Add-Type&quot;:http://poshcode.org/720 on top of the script I presented.


There is one _other_ reason to use the Add-Type version, of course: when you need to call the actual &quot;shift operators&quot;:http://msdn.microsoft.com/en-us/library/aa691377(VS.71).aspx on a type that overrides them with a custom implementation.</description>
		<content:encoded><![CDATA[<p>Wow, nice work <img src='http://huddledmasses.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The speed is only a hair off the Add-Type one I wrote, as far as I can tell &#8230; but yeah, that&#8217;s about as complicated as things come.  I think either one would work, but yours is a lot easier to use on PowerShell v1, since you wouldn&#8217;t need the <a href="http://poshcode.org/720">New-Type script to stand in for Add-Type</a> on top of the script I presented.</p>
<p>There is one <em>other</em> reason to use the Add-Type version, of course: when you need to call the actual <a href="http://msdn.microsoft.com/en-us/library/aa691377(VS.71">shift operators</a>).aspx on a type that overrides them with a custom implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed Withers</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212260</link>
		<dc:creator>Ed Withers</dc:creator>
		<pubDate>Fri, 27 Feb 2009 21:07:32 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212260</guid>
		<description>Hi Joel,

Ok, the more I think about it, the more I agree with your original premise, we need shift operators.  

Actually, you really caught my attention when you pointed out that PowerShell doesn&#039;t do &quot;integer math&quot;, so I had to do some playing around, and you know how that can lead into strange directions, (like how Int16 behaves differently than Int32 when you try to assign values with the high bit set.)

To make a long story short, I decided to play with the bitwise operators and see if I could do the bit truncation needed to make my original multiply/divide trick work.  It&#039;s a lot more work than it&#039;s worth. However, I finally hacked something together that I think is pretty stable and works quickly for any value in a [byte], [Int16], [Int32], or [Int64] regardless of the starting value or number of bits to shift.  

Here&#039;s my code.  I didn&#039;t try to make it pipeline friendly, maybe I&#039;ll save that for another day.  I did combine both left and right shift into a single function with -left and -right switches...

&lt;code lang=&quot;posh&quot;&gt;
function Shift-Bit {
######################################################################
##
##   Shift-Bit
##
##   Accepts an input value in any of the integer types (byte, Int16,
##       Int32, and Int64) and the number of bits to shift it to the left
##
##   Input:
##       $x -    Value in any of the integer types 
##               (byte, Int16, Int32, and Int64)
##               [Defaults to 1]
##
##       $y -    Number of bits to shift between 1 and bitsize of $x -1
##
##       $left - Switch to do left shift
##       $right- Switch to do right shift
##               [Default is right, if both are set, right takes priority]
##
##   v1  B. Edward Withers 27-Feb-2009
##       Initial code
##
#######################################################################
    param ($x = 1, $y, [switch] $left, [switch] $right)

    # PowerShell does some strange casting of Int16, so we have to handle
    # it specially.
    [bool] $flagInt16 = $false

    # Only process if the number of bits to shift is valid
    if ($y -gt 0) {

        # Check the input variable type
        Switch ($x.gettype().fullname) {
            System.Byte {
                $maxbits = 8
                [byte] $highbit = 0x80
                [byte] $allbits = 0xFF
                [byte] $maskclear = 1
                [byte] $maskhigh = 1
                [byte] $maskrest = 1
            }
            System.Int16 {
                $maxbits = 16
                [int16] $highbit = -1 * 0x8000  # Int16 behaves differently
                [int16] $allbits = -1
                [int16] $maskclear = 1
                [int16] $maskhigh = 1
                [int16] $maskrest = 1
                [bool] $flagInt16 = $true
            }
            System.Int32 {
                $maxbits = 32
                [int32] $highbit = 0x80000000
                [int32] $allbits = -1
                [int32] $maskclear = 1
                [int32] $maskhigh = 1
                [int32] $maskrest = 1
            }
            System.Int64 {
                $maxbits = 64
                [int64] $highbit = 0x8000000000000000
                [int64] $allbits = -1
                [int64] $maskclear = 1
                [int64] $maskhigh = 1
                [int64] $maskrest = 1
            }
            # For any other variable type, alert the user and exit
            default {
                &quot;Cannot process&quot; + ($x.gettype().fullname) + &quot; variables in a bitwise fashion.&quot;
                return
            }
        } # End of Switch

        # Only process if the shift distance is valid (less than variable length)
        if ($y -lt $maxbits) {

            # Check the shift direction, default to right, and if both are set,
            # give precedence to right
            if ($left.IsPresent -and (-not $right.IsPresent)) {

                # create a bitmask for just the bits we want to keep
                for ($count=1; $count -le $maxbits - $y -1; $count += 1) {
                    $maskhigh = $maskhigh * 2 # a mask with only the high bit set
                    $maskrest = $maskrest * 2 + 1 # a mask with all kept bits set
                }

                # Now do some mask magic to get the high bits and the rest of the bits
                # separated into variables
                $maskrest = $maskrest -bxor $maskhigh # clear the high bit from $maskrest
                $maskrest = $maskrest -band $x # then get just those bits from input
                $maskhigh = $maskhigh -band $x # and get the high kept bit from input 

                # Now shift left by multiplying by 2
                for ($count=1; $count -le $y; $count += 1) {
                    $maskrest = $maskrest * 2
                }

                # Set the low-order bits of the return value
                $x = $maskrest

                # if the high input bit was set, then set it on output
                if ($maskhigh -ne 0) { 
                    $x = $highbit -bor $x
                }
             }
             else { # Assume we want a right shift if Left wasn&#039;t selected
                # create a bitmask for the bits we will be losing 
                # (this prevents underflow and conversion into a floating point 
                # format)
                for ($count=1; $count -le $y -1; $count += 1) {
                    $maskclear = $maskclear * 2 + 1 # a mask of bits to clear
                }
                # Invert the mask so the bits to clear are zero
                $maskclear = $maskclear -bxor $allbits
                # Reduce the input to just the bits we want
                $maskclear = $maskclear -band $x
                # Now shift to the right by dividing by 2
                for ($count=1; $count -le $y; $count += 1) {
                    $maskclear = $maskclear / 2
                }
                
                # Set the return value
                $x = $maskclear                
             }
        }
        else {
            &quot;Cannot shift a &quot; + ($x.gettype().fullname) + &quot; by $y bits.&quot;
        }
    } # End of check for shift length

    # Handle the special case by forcing the output to be cast back
    if ($flagInt16) {[Int16]$x = $x}

    # Output the result
    $x

} # End of Function&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hi Joel,</p>
<p>Ok, the more I think about it, the more I agree with your original premise, we need shift operators.  </p>
<p>Actually, you really caught my attention when you pointed out that PowerShell doesn&#8217;t do &#8220;integer math&#8221;, so I had to do some playing around, and you know how that can lead into strange directions, (like how Int16 behaves differently than Int32 when you try to assign values with the high bit set.)</p>
<p>To make a long story short, I decided to play with the bitwise operators and see if I could do the bit truncation needed to make my original multiply/divide trick work.  It&#8217;s a lot more work than it&#8217;s worth. However, I finally hacked something together that I think is pretty stable and works quickly for any value in a [byte], [Int16], [Int32], or [Int64] regardless of the starting value or number of bits to shift.  </p>
<p>Here&#8217;s my code.  I didn&#8217;t try to make it pipeline friendly, maybe I&#8217;ll save that for another day.  I did combine both left and right shift into a single function with -left and -right switches&#8230;</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #666699; font-weight: bold;">function</span> Shift<span style="color: #66cc66;">-</span>Bit <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">######################################################################</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; Shift-Bit</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; Accepts an input value in any of the integer types (byte, Int16,</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; Int32, and Int64) and the number of bits to shift it to the left</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; Input:</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; $x - &nbsp; &nbsp;Value in any of the integer types </span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (byte, Int16, Int32, and Int64)</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Defaults to 1]</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; $y - &nbsp; &nbsp;Number of bits to shift between 1 and bitsize of $x -1</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; $left - Switch to do left shift</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; $right- Switch to do right shift</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Default is right, if both are set, right takes priority]</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; v1 &nbsp;B. Edward Withers 27-Feb-2009</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; Initial code</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">#######################################################################</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">param</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$y</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$left</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$right</span><span style="color: #333;">&#41;</span></p>
<p>&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># PowerShell does some strange casting of Int16, so we have to handle</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># it specially.</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">bool</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$flagInt16</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span></p>
<p>&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Only process if the number of bits to shift is valid</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Check the input variable type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">Switch</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span>.<span style="color: #003366;">gettype</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">fullname</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">System</span>.<span style="color: #003366; font-weight: bold;">Byte</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maxbits</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">8</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$highbit</span> <span style="color: #66cc66;">=</span> 0x80<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$allbits</span> <span style="color: #66cc66;">=</span> 0xFF<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">System</span>.<span style="color: #003366;">Int16</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maxbits</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int16<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$highbit</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">*</span> 0x8000 &nbsp;<span style="color: #666666; font-style: italic;"># Int16 behaves differently</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int16<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$allbits</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int16<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int16<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int16<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">bool</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$flagInt16</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">System</span>.<span style="color: #003366;">Int32</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maxbits</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">32</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int32<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$highbit</span> <span style="color: #66cc66;">=</span> 0x80000000<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int32<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$allbits</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int32<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int32<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int32<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">System</span>.<span style="color: #003366;">Int64</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maxbits</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">64</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int64<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$highbit</span> <span style="color: #66cc66;">=</span> 0x8000000000000000<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int64<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$allbits</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int64<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int64<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>int64<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># For any other variable type, alert the user and exit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Cannot process&quot;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span>.<span style="color: #003366;">gettype</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">fullname</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #009900;">&quot; variables in a bitwise fashion.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">return</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666666; font-style: italic;"># End of Switch</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Only process if the shift distance is valid (less than variable length)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$maxbits</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Check the shift direction, default to right, and if both are set,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># give precedence to right</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$left</span>.<span style="color: #003366;">IsPresent</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #333399; font-weight: bold; font-style: italic;">not</span> <span style="color: #660033; font-weight: bold;">$right</span>.<span style="color: #003366;">IsPresent</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># create a bitmask for just the bits we want to keep</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #000066;">-le</span> <span style="color: #660033; font-weight: bold;">$maxbits</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #666666; font-style: italic;"># a mask with only the high bit set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #666666; font-style: italic;"># a mask with all kept bits set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Now do some mask magic to get the high bits and the rest of the bits</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># separated into variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #000066;">-bxor</span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #666666; font-style: italic;"># clear the high bit from $maskrest</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #000066;">-band</span> <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #666666; font-style: italic;"># then get just those bits from input</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #000066;">-band</span> <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #666666; font-style: italic;"># and get the high kept bit from input </span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Now shift left by multiplying by 2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #000066;">-le</span> <span style="color: #660033; font-weight: bold;">$y</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskrest</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Set the low-order bits of the return value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskrest</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># if the high input bit was set, then set it on output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$maskhigh</span> <span style="color: #000066;">-ne</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$highbit</span> <span style="color: #000066;">-bor</span> <span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #666666; font-style: italic;"># Assume we want a right shift if Left wasn't selected</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># create a bitmask for the bits we will be losing </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># (this prevents underflow and conversion into a floating point </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># format)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #000066;">-le</span> <span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #666666; font-style: italic;"># a mask of bits to clear</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Invert the mask so the bits to clear are zero</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #000066;">-bxor</span> <span style="color: #660033; font-weight: bold;">$allbits</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Reduce the input to just the bits we want</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #000066;">-band</span> <span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Now shift to the right by dividing by 2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #000066;">-le</span> <span style="color: #660033; font-weight: bold;">$y</span>; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskclear</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Set the return value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$maskclear</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Cannot shift a &quot;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span>.<span style="color: #003366;">gettype</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">fullname</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #009900;">&quot; by $y bits.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666666; font-style: italic;"># End of check for shift length</span></p>
<p>&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Handle the special case by forcing the output to be cast back</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$flagInt16</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Int16<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$x</span><span style="color: #333;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Output the result</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$x</span></p>
<p><span style="color: #333;">&#125;</span> <span style="color: #666666; font-style: italic;"># End of Function</span></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel 'Jaykul' Bennett</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212145</link>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
		<pubDate>Mon, 23 Feb 2009 21:23:28 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212145</guid>
		<description>Well, no, using math wouldn&#039;t be easier ... because PowerShell doesn&#039;t seem to have integer multiplication, so your version will incorrectly overflow into a double. Lets call yours Shift-LeftMath, and try this:

&lt;code lang=&quot;posh&quot;&gt;
[1]: $x = get-random; $x
1766273347
[2]: Shift-Left $x 1
-762420602
[3]: Shift-LeftMath $x 1
3532546694  &lt;/code&gt;

Of course, the same thing happens in the other direction:

&lt;code lang=&quot;posh&quot;&gt;
[4]: Shift-RightMath 42 4
2.625&lt;/code&gt;

You can fix the shift-right easily by using either @[Math]::DivRem@ or @[Math]::Floor@ ... but shift-left is trickier, because you have to detect the overflow and truncate bits, or else you&#039;re not doing the same left shift that you would be doing in C# (or any other programming language?).  If I thought through all of that (as I just did, thanks to you), I could have written something like this (notice I made these pipeline capable the same as the originals posted above):

&lt;code lang=&quot;posh&quot;&gt;
function Shift-LeftMath {
Param ($x =1, $y)
BEGIN {
   if($y) {
      $result = $x
      (1..$y)&#124;%{$result = [Math]::Floor($result * 2)}
      while($result -gt [int]::MaxValue) { 
         $result = $result - [int]::MaxValue - [int]::MaxValue - 2  
      } 
      $result
   }
}
PROCESS {
   if($_){
      $result,$y = $_,$x
      (1..$y)&#124;%{$result = [Math]::Floor($result * 2)}
      while($result -gt [int]::MaxValue) { 
         $result = $result - [int]::MaxValue - [int]::MaxValue - 2  
      } 
      Write-Host
      $result
   }
}
}


function Shift-RightMath {
Param ($x =1, $y)
BEGIN {
   if($y) {
      $result = $x
      (1..$y)&#124;%{$result = [Math]::Floor($result / 2)}
      $result
   }
}
PROCESS {
   if($_){
      $result,$y = $_,$x
      (1..$y)&#124;%{$result = [Math]::Floor($result / 2)}
      $result
   }
}
}&lt;/code&gt;

But after all of that, the math version still takes many times as long to run, even for the Shift-Right.  The Shift-Left can take a _very_ long time, because the solution I came up with for rounding down involves iterating in a while loop and if you shifted a large number 16 bits left it can take thousands of loops to come clean. I&#039;m sure there&#039;s a better way than that, but I can&#039;t think of one right now.  Anyway, here&#039;s a comparison of the relatively fast Shift-Right, doing the task I started out trying to do -- get the high-order word:

&lt;code lang=&quot;posh&quot;&gt;
$val = [System.Windows.Media.RenderCapability]::Tier
1..1000 &#124; %{ $x = Shift-Right $val 16 }; $x
1..1000 &#124; %{ $x = Shift-RightMath $val 16 }; $x
&lt;/code&gt;

notextile. &lt;code&gt;&lt;pre&gt;
Duration Average Commmand
-------- ------- --------
 0.23438 0.00023 1..1000 &#124; %{ $x = Shift-Right $val 16 }; $x
 1.65625 0.00166 1..1000 &#124; %{ $x = Shift-RightMath $val 16 }; $x
 &lt;/pre&gt;&lt;/code&gt;
 
Obviously the actual time is tiny, if you were doing thousands of these, you&#039;d regret it -- and as I said before, I can&#039;t find a way to make the mathematical left shift do the right thing when it overflows that doesn&#039;t take at least half a second for a simple @Shift-LeftMath 0x42000001 16@:

notextile. &lt;code&gt;&lt;pre&gt;
Duration Average Commmand
-------- ------- --------
 0.43750 0.43750 Shift-LeftMath 0x42000001 16
 0.00000 0.00000 Shift-Left 0x42000001 16
&lt;/pre&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Well, no, using math wouldn&#8217;t be easier &#8230; because PowerShell doesn&#8217;t seem to have integer multiplication, so your version will incorrectly overflow into a double. Lets call yours Shift-LeftMath, and try this:</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>: <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">random</span></span>; <span style="color: #660033; font-weight: bold;">$x</span><br />
<span style="color: #cc66cc;">1766273347</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#93;</span>: Shift<span style="color: #66cc66;">-</span>Left <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">762420602</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #333;">&#93;</span>: Shift<span style="color: #66cc66;">-</span>LeftMath <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #cc66cc;">3532546694</span> &nbsp;</div>
<p>Of course, the same thing happens in the other direction:</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #333;">&#93;</span>: Shift<span style="color: #66cc66;">-</span>RightMath <span style="color: #cc66cc;">42</span> <span style="color: #cc66cc;">4</span><br />
<span style="color: #cc66cc;">2.625</span></div>
<p>You can fix the shift-right easily by using either <code>[Math]::DivRem</code> or <code>[Math]::Floor</code> &#8230; but shift-left is trickier, because you have to detect the overflow and truncate bits, or else you&#8217;re not doing the same left shift that you would be doing in C# (or any other programming language?).  If I thought through all of that (as I just did, thanks to you), I could have written something like this (notice I made these pipeline capable the same as the originals posted above):</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #666699; font-weight: bold;">function</span> Shift<span style="color: #66cc66;">-</span>LeftMath <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span>1..<span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Math<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Floor</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #000066;">-gt</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">-</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span> <span style="color: #66cc66;">-</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span>,<span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>,<span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span>1..<span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Math<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Floor</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #000066;">-gt</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">-</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span> <span style="color: #66cc66;">-</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">MaxValue</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></p>
<p>
<span style="color: #666699; font-weight: bold;">function</span> Shift<span style="color: #66cc66;">-</span>RightMath <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span>1..<span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Math<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Floor</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span>,<span style="color: #660033; font-weight: bold;">$y</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>,<span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span>1..<span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Math<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Floor</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>
<p>But after all of that, the math version still takes many times as long to run, even for the Shift-Right.  The Shift-Left can take a <em>very</em> long time, because the solution I came up with for rounding down involves iterating in a while loop and if you shifted a large number 16 bits left it can take thousands of loops to come clean. I&#8217;m sure there&#8217;s a better way than that, but I can&#8217;t think of one right now.  Anyway, here&#8217;s a comparison of the relatively fast Shift-Right, doing the task I started out trying to do &#8212; get the high-order word:</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #660033; font-weight: bold;">$val</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">System</span>.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Media</span>.<span style="color: #003366;">RenderCapability</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Tier</span><br />
1..1000 <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> Shift<span style="color: #66cc66;">-</span>Right <span style="color: #660033; font-weight: bold;">$val</span> <span style="color: #cc66cc;">16</span> <span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$x</span><br />
1..1000 <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> Shift<span style="color: #66cc66;">-</span>RightMath <span style="color: #660033; font-weight: bold;">$val</span> <span style="color: #cc66cc;">16</span> <span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$x</span><br />
&nbsp;</div>
<p><code>
<pre>
Duration Average Commmand
-------- ------- --------
 0.23438 0.00023 1..1000 | %{ $x = Shift-Right $val 16 }; $x
 1.65625 0.00166 1..1000 | %{ $x = Shift-RightMath $val 16 }; $x
 </pre>
<p></code></p>
<p>Obviously the actual time is tiny, if you were doing thousands of these, you&#8217;d regret it &#8212; and as I said before, I can&#8217;t find a way to make the mathematical left shift do the right thing when it overflows that doesn&#8217;t take at least half a second for a simple <code>Shift-LeftMath 0x42000001 16</code>:</p>
<p><code>
<pre>
Duration Average Commmand
-------- ------- --------
 0.43750 0.43750 Shift-LeftMath 0x42000001 16
 0.00000 0.00000 Shift-Left 0x42000001 16
</pre>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed Withers</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212139</link>
		<dc:creator>Ed Withers</dc:creator>
		<pubDate>Mon, 23 Feb 2009 17:21:08 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212139</guid>
		<description>Ok, I take the point, and I&#039;ll grant that Add-Type is cool, but wouldn&#039;t it have been easier to just:

&lt;code lang=&quot;posh&quot;&gt;
function shift-left {
  Param ($x =1, $y)
  (1..$y)&#124;%{$x = $x * 2}
  $x
}&lt;/code&gt;

(and divide for shift-right)?</description>
		<content:encoded><![CDATA[<p>Ok, I take the point, and I&#8217;ll grant that Add-Type is cool, but wouldn&#8217;t it have been easier to just:</p>
<div class="posh code posh" style="font-family:monospace;">
<span style="color: #666699; font-weight: bold;">function</span> shift<span style="color: #66cc66;">-</span>left <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">Param</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><br />
&nbsp; <span style="color: #333;">&#40;</span>1..<span style="color: #660033; font-weight: bold;">$y</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$x</span><br />
<span style="color: #333;">&#125;</span></div>
<p>(and divide for shift-right)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel 'Jaykul' Bennett</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212122</link>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
		<pubDate>Mon, 23 Feb 2009 04:58:53 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212122</guid>
		<description>Add-Type is awesome, but it would be really cool if we could add more operators ourselves ... how is it that shift operators aren&#039;t in, but -split and -join are *operators*?</description>
		<content:encoded><![CDATA[<p>Add-Type is awesome, but it would be really cool if we could add more operators ourselves &#8230; how is it that shift operators aren&#8217;t in, but -split and -join are <strong>operators</strong>?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Snover</title>
		<link>http://huddledmasses.org/powershell-needs-shift-operators/comment-page-1/#comment-212121</link>
		<dc:creator>Jeffrey Snover</dc:creator>
		<pubDate>Mon, 23 Feb 2009 04:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://huddledmasses.org/?p=1094#comment-212121</guid>
		<description>Boy I&#039;m glad we added Add-Type to V2!</description>
		<content:encoded><![CDATA[<p>Boy I&#8217;m glad we added Add-Type to V2!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
