bit-wise operators for bigint in Clojure

212 views
Skip to first unread message

Harmon Nine

unread,
May 26, 2020, 12:39:51 PM5/26/20
to Clojure
Hello.

I noticed a post about this from 2013, so doing a bump.

The bit-wise operators appear to be supported for 'bigint' in ClojureScript, but not in Clojure.  Is support for these operations on 'bigint' in Clojure a future enhancement?

Thanks :)
-- Harmon

Alan Thompson

unread,
May 28, 2020, 4:28:41 PM5/28/20
to clojure
In Clojure, it is probably easiest to just use Java interop, eg:  https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html#setBit(int)
Alan


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/4b6e3f5e-c70c-404a-9348-8117a4b32db8%40googlegroups.com.
Message has been deleted

Niels van Klaveren

unread,
Jun 2, 2020, 9:11:53 AM6/2/20
to Clojure
Clojure often leaves implementations to the backing platforms, and Clojurescript probably has no difference in the methods of the different numerical types.
Java does, so you'd have to fall back on that. Something like:

(defn big-or
  [f & r]
  (reduce (fn [acc v] (.or acc (biginteger v))) (biginteger f) r))

(defn big-and
  [f & r]
  (reduce (fn [acc v] (.and acc (biginteger v))) (biginteger f) r))

(defn big-xor
  [f & r]
  (reduce (fn [acc v] (.xor acc (biginteger v))) (biginteger f) r))

A lot of (mathemathical) operations need falling back to the platform, and this is by design for clojure. There are however libraries that are intended to abstract away the differences, and one of those for math is clojure.math.numeric-tower.

A numeric type independent implementation for bitwise operations would be a fitting pull request there.
Reply all
Reply to author
Forward
0 new messages