Page 1 of 1

When does lisp use binary vs. base-10?

Posted: Sat May 11, 2013 10:03 am
by dagst47
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?

Posted: Sat May 11, 2013 8:52 pm
by nuntius
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?

Posted: Sun May 12, 2013 1:50 am
by Goheeca
Exactly as nuntius said. It's like in other languages. For example in C:

Code: Select all

11 // = 11
0x11 // = 17
011 // = 9
In CL you have standard reader macros:

Code: Select all

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:

Code: Select all

(setf *print-base* 16)
(setf *read-base* 16)
(+ a34b 17fe) ; the result is bb49

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

Posted: Sun May 12, 2013 6:52 am
by Kompottkin
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?

Posted: Mon Jul 08, 2013 7:59 pm
by findinglisp
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.