www.delorie.com/gnu/docs/elisp-manual-21/elisp_58.html   search  
 
Buy the book!


GNU Emacs Lisp Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 Integer Basics

The range of values for an integer depends on the machine. The minimum range is -134217728 to 134217727 (28 bits; i.e., -2**27 to 2**27 - 1), but some machines may provide a wider range. Many examples in this chapter assume an integer has 28 bits.

The Lisp reader reads an integer as a sequence of digits with optional initial sign and optional final period.

 
 1               ; The integer 1.
 1.              ; The integer 1.
+1               ; Also the integer 1.
-1               ; The integer -1.
 268435457       ; Also the integer 1, due to overflow.
 0               ; The integer 0.
-0               ; The integer 0.

In addition, the Lisp reader recognizes a syntax for integers in bases other than 10: `#Binteger' reads integer in binary (radix 2), `#Ointeger' reads integer in octal (radix 8), `#Xinteger' reads integer in hexadecimal (radix 16), and `#radixrinteger' reads integer in radix radix (where radix is between 2 and 36, inclusivley). Case is not significant for the letter after `#' (`B', `O', etc.) that denotes the radix.

To understand how various functions work on integers, especially the bitwise operators (see section 3.8 Bitwise Operations on Integers), it is often helpful to view the numbers in their binary form.

In 28-bit binary, the decimal integer 5 looks like this:

 
0000  0000 0000  0000 0000  0000 0101

(We have inserted spaces between groups of 4 bits, and two spaces between groups of 8 bits, to make the binary integer easier to read.)

The integer -1 looks like this:

 
1111  1111 1111  1111 1111  1111 1111

-1 is represented as 28 ones. (This is called two's complement notation.)

The negative integer, -5, is creating by subtracting 4 from -1. In binary, the decimal integer 4 is 100. Consequently, -5 looks like this:

 
1111  1111 1111  1111 1111  1111 1011

In this implementation, the largest 28-bit binary integer value is 134,217,727 in decimal. In binary, it looks like this:

 
0111  1111 1111  1111 1111  1111 1111

Since the arithmetic functions do not check whether integers go outside their range, when you add 1 to 134,217,727, the value is the negative integer -134,217,728:

 
(+ 1 134217727)
     => -134217728
     => 1000  0000 0000  0000 0000  0000 0000

Many of the functions described in this chapter accept markers for arguments in place of numbers. (See section 31. Markers.) Since the actual arguments to such functions may be either numbers or markers, we often give these arguments the name number-or-marker. When the argument value is a marker, its position value is used and its buffer is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003