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.

When does lisp use binary vs. base-10?

5 posts · 5597 views

Hi all-
I'm on a mission to learn about computers (beyond just using gui programs), and I've decided to tackle LISP.
Working my way through the examples in the book Land of Lisp (by Dr. Barski, written to cover ANSI Common Lisp), I don't understand when lisp evaluates a number as base 10 versus binary.
For example:
(ash 11 -1) yields 5. Why isn't that yielding 1? (ie: 11 binary = 3 base 10. left shift 11 and you get 2^0 , or 1). Instead it wants to convert 11 to binary first (1011) then left shift it to (101) to get 5. Does that strike anyone else as being weird, or is it just me? It seems that if one of the "operands" (sorry if this is the wrong word) is a binary shift, the other "operand" should be binary too, right?
The remainder of the examples in the book seem to use base-10 numbering without converting to binary, so I'm a little confused.
Is there a "rule of thumb" for when lisp evaluates a number's binary representation first versus using base 10?
Thanks!
D

Re: When does lisp use binary vs. base-10?

By default, CL always reads and prints numbers as base-10. Internally, it generally doesn't matter how numbers are stored; but for operations like ASH, they are clearly specified as binary.

Re: When does lisp use binary vs. base-10?

Exactly as nuntius said. It's like in other languages. For example in C:
11 // = 11
0x11 // = 17
011 // = 9
In CL you have standard reader macros:
11 ; = 11
#x11 ; = 17
#o11 ; = 9
#b11 ; = 3
#3r120 ; = 15
You can also change a radix used by reader by setting *read-base* variable and by setting *print-base* you change a radix used by printer:
(setf *print-base* 16)
(setf *read-base* 16)
(+ a34b 17fe) ; the result is bb49
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: When does lisp use binary vs. base-10?

Numbers don't have a base. Printed representations of numbers have a base. Thus, base is relevant only when reading and printing numbers. Decimal 5 is the same number as binary 101 (the number five). It's not a conversion. It's the same number.

Lisp always reads numbers according to the current *read-base* setting, which is usually ten. As pointed out by Goheeca, you can easily input numbers in other bases by using the appropriate read-macros.

Re: When does lisp use binary vs. base-10?

Kompottkin wrote:Numbers don't have a base. Printed representations of numbers have a base. Thus, base is relevant only when reading and printing numbers. Decimal 5 is the same number as binary 101 (the number five). It's not a conversion. It's the same number.
This is a good point. Numbers are really abstract concepts. Number representations are concrete and can have many different forms (and have over the centuries), but a representation is not a number.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/