When does lisp use binary vs. base-10?

Discussion of Common Lisp

When does lisp use binary vs. base-10?

Postby dagst47 » Sat May 11, 2013 10:03 am

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
dagst47
 
Posts: 3
Joined: Sat May 11, 2013 9:36 am

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

Postby nuntius » Sat May 11, 2013 8:52 pm

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.
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

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

Postby Goheeca » Sun May 12, 2013 1:50 am

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
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 198
Joined: Thu May 10, 2012 12:54 pm

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

Postby Kompottkin » Sun May 12, 2013 6:52 am

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.
User avatar
Kompottkin
 
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany

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

Postby findinglisp » Mon Jul 08, 2013 7:59 pm

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/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 4 guests

cron