This can be accomplished by calling the int clojure.lang.Numbers.unsignedShiftRightInt(int, int) method, which uses the arguments >>> on int , returning an int . Currently, it does not appear as a function anywhere, but it has a built-in implementation (equivalent to >>> in Java), and you can either call it directly or wrap it in your built-in function:
(defn unsigned-bit-shift-right-int {:inline (fn [xn] `(clojure.lang.Numbers/unsignedShiftRightInt ~x ~n))} [xn] (clojure.lang.Numbers/unsignedShiftRightInt xn))
This returns the correct value whether it is inserted or not, but of course you usually want it to be embedded. It is also useful to make sure that the arguments are actually primitive int , so that the inside can hit.
Here it compiles in Clojure 1.8 in two possible cases when it becomes nested (a non-built-in case is a regular function call, nothing is visible there):
Introduced with primitive arguments:
Violation of count bit to illustrate this point. Pay attention to the iushr instruction.
Nested with non-primitive arguments:
Pay attention to the invokestatic clojure.lang.Numbers.unsignedShiftRight… instruction.
Clojure expression:
#(format "%08x" (clojure.lang.Numbers/unsignedShiftRightInt (unchecked-int 0xf2345678) 4))
Bytecode:
// Method descriptor #11 ()Ljava/lang/Object; // Stack: 5, Locals: 1 public java.lang.Object invoke(); 0 getstatic user$eval16141$fn__16142.const__0 : clojure.lang.Var [15] 3 invokevirtual clojure.lang.Var.getRawRoot() : java.lang.Object [20] 6 checkcast clojure.lang.IFn [22] 9 ldc <String "%08x"> [24] 11 ldc2_w <Long 4063516280> [25] 14 l2i 15 ldc2_w <Long 4> [27] 18 invokestatic clojure.lang.RT.intCast(long) : int [34] 21 invokestatic clojure.lang.Numbers.unsignedShiftRightInt(int, int) : int [40] 24 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [46] 27 invokeinterface clojure.lang.IFn.invoke(java.lang.Object, java.lang.Object) : java.lang.Object [49] [nargs: 3] 32 areturn Line numbers: [pc: 0, line: 1] [pc: 6, line: 1] [pc: 14, line: 1] [pc: 21, line: 1] [pc: 27, line: 1] Local variable table: [pc: 0, pc: 32] local: this index: 0 type: java.lang.Object
source share