diff --git a/pyopencl/algorithm.py b/pyopencl/algorithm.py
index bedfb1a0851e85d11603d1b861edd79a6936241c..48cb2f048b4f1e4e292f1f58bc321f5f850d818c 100644
--- a/pyopencl/algorithm.py
+++ b/pyopencl/algorithm.py
@@ -249,13 +249,11 @@ def unique(ary, is_equal_expr="a == b", extra_args=[], preamble="",
 def to_bin(n):
     # Py 2.5 has no built-in bin()
     digs = []
-    while True:
+    while n:
         digs.append(str(n % 2))
-        n /= 2
-        if not n:
-            break
-    digs.reverse()
-    return ''.join(digs)
+        n >>= 1
+
+    return ''.join(digs[::-1])
 
 
 def _padded_bin(i, l):