About Monkey 2 › Forums › Monkey 2 Programming Help › Bytes 'n bytes
This topic contains 22 replies, has 4 voices, and was last updated by
Gwenel
12 months ago.
-
AuthorPosts
-
April 23, 2018 at 10:13 pm #14484
Here is one way to do SAR if anyone needs it. It could use optimisation if speed is an issue but it’s a start.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Namespace myapp#Import "<std>"Using std..' Sar (Bit shifting right that saves the sign bit)Function Main()' 100 Sar 2Print Sar(100:UByte , 2:UByte)Print Sar(100:UShort , 2:UShort)Print Sar(100:UInt , 2:UInt)' Print Sar(100:ULong , 2:ULong)End' 8-bit SARFunction Sar:UByte(value:UByte,width:UByte)Local sign : UByte = value & $80value = value Shr widthIf sign Then value |= (255 Shl (8 - width))Return valueEnd Function' 16-bit SARFunction Sar:UShort(value:UShort,width:UShort)Local sign : UShort = value & $8000value = value Shr widthIf sign Then value |= (65535 Shl (16 - width))Return valueEnd Function' 32-bit SARFunction Sar:UInt(value:UInt,width:UInt)Local sign : UInt = value & $80000000value = value Shr widthIf sign Then value |= ( 4294967295 Shl (32 - width))Return valueEnd Function' 64-bit SAR (Gives error, Long seems to be the highest possible number in TED without String trick)Function Sar:ULong(value:ULong,width:ULong)Local sign : ULong = value & $8000000000000000value = value Shr widthIf sign Then value |= ( 18446744073709551615 Shl (64 - width))Return valueEnd FunctionApril 24, 2018 at 12:48 am #14487Thats pretty good. Now all you need is “SAL”.
April 24, 2018 at 1:11 am #14488Nope, SHL & SAL are identical. Lucky us huh?
April 24, 2018 at 1:25 am #14489That’s interesting. Never delved deep enough to notice that.
April 24, 2018 at 2:40 am #14491Here’s a shorter variant.
Monkey12345678910111213141516171819202122232425262728293031Namespace myapp#Import "<std>"Using std..Function Main()' Example of Sar test,2' This implementation uses cheaper instructions but needs a temporary' bigger datatype so a 64-bit wont be happening as easily wo a 128-bit type'Local test:UByte = UByte(StringToULong("11000001",2))Print ULongToString(test,2)Local shifted:= Sar(test,2) ' Shifts 2 steps to the right, keeping the sign-bit (the leftmost bit)Print ULongToString(shifted,2)EndFunction Sar:UByte(value:UByte,s:UByte) ' 8-bitLocal result:UShort = (value ~ 128) - 128Return result Shr sEndFunction Sar:UShort(value:UShort,s:UShort) ' 16-bitLocal result:UInt = (value ~ 32768) - 32768Return result Shr sEndFunction Sar:UInt(value:UInt,s:UInt) ' 32-bitLocal result:ULong = (value ~ 2147483648) - 2147483648Return result Shr sEndApril 24, 2018 at 11:55 am #14494Shr is signed if operands are signed, eg: -64 Shr 1 does a signed shift right (ie: -32) while print UInt(-64) shr 1 does an unsigned shift right.
April 24, 2018 at 2:23 pm #14496Hahaha my god, okay. That’s clever. +1 for that
April 24, 2018 at 2:29 pm #14497You should really document Monkey2 better, these things are gold.
-
AuthorPosts
You must be logged in to reply to this topic.