Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the binary value of a number in clojure

Tags:

clojure

We can represent the number 12 as 2r001100 in clojure.

Is there a built-in function to print 2r001100 when given the number 12?

like image 315
zcaudate Avatar asked Jan 30 '14 05:01

zcaudate


People also ask

How do you print a binary value?

To print binary representation of unsigned integer, start from 31th bit, check whether 31th bit is ON or OFF, if it is ON print “1” else print “0”. Now check whether 30th bit is ON or OFF, if it is ON print “1” else print “0”, do this for all bits from 31 to 0, finally we will get binary representation of number.

How do you print a number in binary in Python?

To print binary value of a given integer, we use bin() function it accepts the number as an argument and returns the binary value. Return Value : A binary string of an integer or int object.


1 Answers

java.lang.Integer/toString will print numbers with arbitrary radix:

(Integer/toString 0xf2 2)  ==> "11110010"
(Integer/toString 0xf2 16) ==> "f2"
(Integer/toString 0xf2 27) ==> "8q"
like image 87
jbouwman Avatar answered Oct 29 '22 21:10

jbouwman