1.3.0 permalink Arrow_down_16x16

bit-shift-left

clojure.core

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

2 Examples top

  • user=> (bit-shift-left 2r1101 2) ; fill rightmost bits with 0s
    52 
    ;; 52 = 2r110100
    
    
  • ;;a bogus bit-array implementation
    
    (def ba (atom (long 0)))
    
    (defn set-ba 
    "sets bit n in long atom ba"
      [n]
      (let [number-set (bit-shift-left 1 n)
    	_ (println "number to set: " number-set)
    	new-array (bit-or @ba number-set)]
        (reset! ba new-array)))
    
    (defn get-ba 
    "gets bit n in long atom ba"
    [n]
      (not (zero? (bit-and (bit-shift-left 1 n) @ba))))
    
    (comment
      (set-ba 0) ;; 0 [....0001]
      (set-ba 3) ;; 2^3 = 8  [....1001]
      (get-ba 0) ;; (bit-and ba 2^0) = 1
      (get-ba 1) ;; (bit-and ba 2^1) = 0
      (get-ba 3) ;; (bit-and ba 2^3) = 1
      ;;but:
      (set-ba 65) ;; [....1011]
      ;;number to set:  2
      ;;modulo because long has only 64 bit
      ;;also note that long always is two-complemented (signed) in java implementation
      )
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:1291 top

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

Comments top

No comments for bit-shift-left. Log in to add a comment.