This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Binary operations in Lisp

4 posts · 4771 views

hello everyone. i am new to Lisp. i dont know how to perform binary operations in LISP?
can anyone share sample of code for binary addition or multiplication plz

Re: Binary operations in Lisp

A binary operation is treated as any other n-ary operation. There is nothing special about it. Use prefix notation:
(+ 1 2) ; = 3
(* 2 3) ; = 6
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Binary operations in Lisp

Integers don't have base when they are stored. Your reader can read numbers in several different bases.
(+ 10 #xa #b1010) ; ==> 30 
Now when makeing a string out of the number you get to choose the base:
(let ((thirty (+ 10 #xa #b1010)))
  (format nil "~n ~x ~o ~b" thirty thirty thirty thirty))
; ==> "30 1E 36 11110"
There you have it. 30 displayed in 4 different bases. In CL you have the special variable *print-base* which sets the default base in which numbers are printed and *read-base* which sets the default base in which numbers are read when parsed:
(let ((*print-base* 16)
        (*read-base* 2))
  (princ (read-from-string "1011"))) ; ==> B
Thats about all there is to know about displaying and parsing numbers. Now all your base are belong to you :-)
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: Binary operations in Lisp

I think the question was about binary operations (operations that take exactly two arguments), not how to compute numbers represented in binary format. And the question smells very much like homework...