DEV Community

Discussion on: Daily Challenge #255 - Is There an Odd Bit?

Collapse
 
quoll profile image
Paula Gearon

Since all integers and longs are signed in the JVM, this makes the high order bit awkward to work with in Clojure. Unlike Java, all integral numbers are assumed to be long, so there is no special syntax to indicate this. And when numbers get large enough, they automatically roll over into BigInt values.

Consequently, even though Java can say:

jshell> 0xAAAAAAAAAAAAAAAAL
$1 ==> -1

Clojure does not have this option of indicating that number is a long (and hence negative because of the most significant bit):

user=> 0xAAAAAAAAAAAAAAAA
12297829382473034410N

(Note: the N indicates a BigInt value)

So we need to use the 2s complement form, and negate the value:

(defn any-odd [x] (not (zero? (bit-and -0x5555555555555556 x))))