Feature Request: Logical Shift Right.

Monkey Forums/Monkey Programming/Feature Request: Logical Shift Right.

FlameDuck(Posted 2011) [#1]
Is there any way to do a LSR (or >>> in Java/JavaScript)? The documentation only documents Shl and Shr as operators which is a left shift and arithmetic shift right respectively.


Goodlookinguy(Posted 2011) [#2]
To answer your question, no, Monkey doesn't have this.

Now for my comments on it. To me it doesn't make any sense to have this, seeing as int is a signed integer. What good would it do to have a logical shift? If there's a compelling argument, I might understand, otherwise it seems pointless. I've seldom needed to shift bits in my years of programming, and never needed logical shift ever. So I'd be interested to here what kinds of uses you would have for this.


FlameDuck(Posted 2011) [#3]
Now for my comments on it. To me it doesn't make any sense to have this, seeing as int is a signed integer. What good would it do to have a logical shift?
Because monkey doesn't have an unsigned Int type.

So I'd be interested to here what kinds of uses you would have for this.
The immediate need is for a CRC-32 implementation. There are lots of algorithms that require logical shifts, that you've never personally had to use it seems irrelevant (anecdotes are not data) considering that languages like Java which (like Monkey) only has signed ints, has a specific operator explicitly for this purpose (because simply casting the first operator to an unsigned int, like you would in C# and C++ obviously won't work).


Goodlookinguy(Posted 2011) [#4]
I see, the intention is to shift the type to an unsigned int. Wasn't really thinking before I wrote that, coming from C# and C++. I wonder if you could just implement this yourself by hand? It wouldn't be a terribly daunting task now would it? Or maybe it's because of my lack of using the languages AS3, Java and Objective-C. I would look it up, but I'm heading off to sleep. So I'll come back tomorrow and look around if someone hasn't already figured out a solution by then.


Floyd(Posted 2011) [#5]
I was kind of surprised at the lack of a logical shift right.

But if you really need it just set the sign bit to zero by doing a bitwise And with $7FFFFFFF before right shifting.


Goodlookinguy(Posted 2011) [#6]
Edit: Okay, I made one for most of the outputs except iOS and that's because I've never touched it.

Note: Lsl performs the same action as just using Shl. I just wanted to be consistent with left and right shifting actions.

First created a folder called "Extra" in the modules area.

Extra.monkey
Import "native/extra.${TARGET}.${LANG}"

Extern

#If LANG<>"cpp"
	Function Lsr:Int( number:Int, shiftBy:Int ) = "Extra.Lsr"
	Function Lsl:Int( number:Int, shiftBy:Int ) = "Extra.Lsl"
#Else
	Function Lsr:Int( number:Int, shiftBy:Int ) = "Extra::Lsr"
	Function Lsl:Int( number:Int, shiftBy:Int ) = "Extra::Lsl"
#End


Then made a folder inside of it called "native"

Inside native...

extra.xna.cs
class Extra
{
	public static int Lsr( int number, int shiftBy )
	{
		return (int)((uint)number >> shiftBy);
	}
	public static int Lsl( int number, int shiftBy )
	{
		return (int)((uint)number << shiftBy);
	}
}
extra.html5.js
var Extra = 
{
	Lsr : function( number, shiftBy )
	{
		return number >>> shiftBy;
	},
	Lsl : function( number, shiftBy )
	{
		return number << shiftBy;
	}
}
extra.glfw.cpp
typedef unsigned int uint;

class Extra
{
	public:
	
	static int Lsr( int number, int shiftBy )
	{
		return (int)((uint)number >> shiftBy);
	}
	
	static int Lsl( int number, int shiftBy )
	{
		return (int)((uint)number << shiftBy);
	}
};
extra.android.java
class Extra
{
	public static int Lsr( int number, int shiftBy)
	{
		return number >>> shiftBy;
	}
	public static int Lsl( int number, int shiftBy )
	{
		return number << shiftBy;
	}
}
extra.flash.as
class Extra
{
	public static function Lsr( number:int, shiftBy:int ):int
	{
		return number >>> shiftBy;
	}
	public static function Lsl( number:int, shiftBy:Number ):int
	{
		return number << shiftBy;
	}
}
Added 4/20/2012 - extra.bmax.bmx
Type Extra
	Function Lsr:Int( number:Int, shiftBy:Int )
		Return number Shr shiftBy
	End Function
	
	Function Lsl:Int( number:Int, shiftBy:Int )
		Return number Shl shiftBy
	End Function
End Type