Unsigned Shift Right
Monkey Forums/Monkey Programming/Unsigned Shift Right
| ||
Can we please get an unsigned shift right operator? Something like "Shru". Also, fix the description of Shr on the wiki, because at the moment it defines Shr as "Bitwise shift left (signed)". |
| ||
Do all targets support unsigned shifting?:Shl Bitwise shift-left :Shr Bitwise shift-right :Sar Bitwise arithmetic shift-right |
| ||
If the language doesn't natively support it, it's not hard to do it manually. |
| ||
In BlitzMax Shr is "unsigned" and Sar is "signed". |
| ||
Ahh... looks like we have a small bug in MonkeyMax then :P BlitzMax: [bbcode]Local a:Int = -858993460 ' binary: 11001100110011001100110011001100 Local o1:Int = a Shl 6 ' left shift Local o2:Int = a Shr 6 ' unsigned right shift Local o3:Int = a Sar 6 ' signed right shift Print ("a = "+a) Print ("ol1 = "+o1) Print ("ol2 = "+o2) Print ("ol3 = "+o3)[/bbcode] Outputs: a = -858993460 ol1 = 858993408 ol2 = 53687091 ol3 = -13421773 Monkey: [monkeycode]Function Main() Local a:Int = -858993460 ' binary: 11001100110011001100110011001100 Local o1:Int = a Shl 6 ' left shift Local o2:Int = a Shr 6 ' signed right shift 'Local o3:Int = a Sar 6 ' signed right shift Print ("a = "+a) Print ("ol1 = "+o1) Print ("ol2 = "+o2) 'Print ("ol3 = "+o3) End[/monkeycode] Outputs a = -858993460 ol1 = 858993408 ol2 = -13421773 I converted this Java example: http://www.herongyang.com/Java/Byte-Data-Type-Shift-Operations.html /** * ShiftOperations.java * Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/ */ public class ShiftOperations { public static void main(String[] arg) { int a = -858993460; // binary: 11001100110011001100110011001100 int o1 = a << 6; // left shift int o2 = a >> 6; // right shift int o3 = a >>> 6; // unsigned right shift System.out.println(" a: "+getBitString(a) +" = "+a); System.out.println("o1: "+getBitString(o1) +" = "+o1); System.out.println("o2: "+getBitString(o2) +" = "+o2); System.out.println("o3: "+getBitString(o3) +" = "+o3); } private static String getBitString(int x) { StringBuffer buf = new StringBuffer(); for (int i=1; i<=32; i++) buf.append(x>>>(32-i) & 0x00000001); return buf.toString(); } } The output of the test program matches my earlier examples: a: 11001100110011001100110011001100 = -858993460 o1: 00110011001100110011001100000000 = 858993408 o2: 11111111001100110011001100110011 = -13421773 o3: 00000011001100110011001100110011 = 53687091 |
| ||
I made logical shift functions a while ago here: http://monkeycoder.co.nz/Community/posts.php?topic=1707#28645 I even updated it with a MonkeyMax target last month. |
| ||
Does this help? (vb) http://pastebin.com/jRteLV58 |
| ||
I even updated it with a MonkeyMax target last month. The fix to MonkeyMax actually needs to be done within the trans target code, because right now if you do a Shr in Monkey it will do a Shr in BlitzMax, whereas it really should convert Shr to Sar. |