1.3.0 permalink Arrow_down_16x16

bit-shift-right

clojure.core

  • (bit-shift-right x n)
Bitwise shift right

2 Examples top

  • ;; Convert number into bits:
    user=> (defn bits [n s]
             (take s
                   (map
                     (fn [i] (bit-and 0x01 i))
                     (iterate
                       (fn [i] (bit-shift-right i 1))
                       n))))
    #'user/bits
    
    user=> (map (fn [n] (bits n 3)) (range 8))
    ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1))
    
  • user=> (bit-shift-right 2r1101 0)
    13
    user=> (bit-shift-right 2r1101 1)
    6
    user=> (bit-shift-right 2r1101 2)
    3
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1297 top

(defn bit-shift-right
  "Bitwise shift right"
  {:inline (fn [x n] `(. clojure.lang.Numbers (shiftRight ~x ~n)))
   :added "1.0"}
  [x n] (. clojure.lang.Numbers shiftRight x n))
Vars in clojure.core/bit-shift-right:
Used in 0 other vars

Comments top

1 comment(s) for bit-shift-right.

From the IRC channel, a way to get zero-fill bit-shift-right:

06:08 < mikera> (defn >>> [v bits] (bit-shift-right (bit-and 0xFFFFFFFF v) bits))

There's also an open ticket for a built-in version.

Log in to add a comment.