返回设置了第一个X位的数字

时间:2022-09-18 22:05:27

what is a more efficient way to accomplish this in clojure:

什么是在clojure中实现这一目标的更有效方法:

(defn ones
  ([n] (ones n 1 1))
  ([n i res]
    (if (< i n)
      (recur n (inc i) (bit-set res i))
      res)))

preferably it should still "do the right thing" when it comes to numerical type.

当涉及到数字类型时,它应该仍然“做正确的事”。

1 个解决方案

#1


Why not take 2^(X-1) (by setting only the Xth bit) and then subtract 1?

为什么不取2 ^(X-1)(通过只设置第X位)然后减去1?

#1


Why not take 2^(X-1) (by setting only the Xth bit) and then subtract 1?

为什么不取2 ^(X-1)(通过只设置第X位)然后减去1?