Skip to main content

ash

ash Function

Syntax:

ash integer count → shifted-integer

Arguments and Values:

integer—an integer .

count—an integer .

shifted-integer—an integer .

Description:

ash performs the arithmetic shift operation on the binary representation of integer, which is treated as if it were binary.

ash shifts integer arithmetically left by count bit positions if count is positive, or right count bit positions if count is negative. The shifted value of the same sign as integer is returned.

Mathematically speaking, ash performs the computation floor(integer·2count). Logically, ash moves all of the bits in integer to the left, adding zero-bits at the right, or moves them to the right, discarding bits.

ash is defined to behave as if integer were represented in two’s complement form, regardless of how integers are represented internally.

Examples:

(ash 16 1)32 
(ash 16 0)16
(ash 16 -1)8
(ash -100000000000000000000000000000000 -100)-79

Exceptional Situations:

Should signal an error of type type-error if integer is not an integer . Should signal an error of type type-error if count is not an integer . Might signal arithmetic-error.

Notes:

(logbitp j (ash n k))

(and (>= j k) (logbitp (- j k) n))

integer-length

Expanded Reference: ash

Shifting left (positive count)

ash performs arithmetic shift. A positive count shifts bits to the left, which is equivalent to multiplying by a power of 2.

(ash 1 4)
=> 16
(ash 16 1)
=> 32
(ash 3 8)
=> 768

Shifting right (negative count)

A negative count shifts bits to the right, which is equivalent to dividing by a power of 2 and taking the floor.

(ash 16 -1)
=> 8
(ash 255 -4)
=> 15
(ash 1 -1)
=> 0

Zero shift

A count of zero returns the integer unchanged.

(ash 16 0)
=> 16
(ash -5 0)
=> -5
(ash 0 100)
=> 0

Negative integers

ash treats integers as if in two's complement form. Shifting a negative integer right preserves the sign (arithmetic shift, not logical shift).

(ash -1 -1)
=> -1
(ash -2 -1)
=> -1
(ash -16 -2)
=> -4
(ash -100000000000000000000000000000000 -100)
=> -79

Practical use: powers of two

ash is an efficient way to compute powers of two.

(ash 1 0)
=> 1
(ash 1 10)
=> 1024
(ash 1 20)
=> 1048576

Relationship to multiplication and division

ash is equivalent to floor(integer * 2^count).

(= (ash 5 3) (* 5 (expt 2 3)))
=> T
(= (ash 100 -3) (floor 100 (expt 2 3)))
=> T