141 lines
8.6 KiB
HTML
141 lines
8.6 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Function BOOLE</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="f_parse_.htm">
|
|
<LINK REL=UP HREF="c_number.htm">
|
|
<LINK REL=NEXT HREF="v_b_1_b.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_parse_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_number.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="v_b_1_b.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="boole"><I>Function</I> <B>BOOLE</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>boole</B> <I>op integer-1 integer-2</I> => <I>result-integer</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>Op</I>---a <A REL=DEFINITION HREF="26_glo_b.htm#bit-wise_logical_operation_specifier"><I>bit-wise logical operation specifier</I></A>. <P>
|
|
<I>integer-1</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>. <P>
|
|
<I>integer-2</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>. <P>
|
|
<I>result-integer</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#boole"><B>boole</B></A> performs bit-wise logical operations on <I>integer-1</I> and <I>integer-2</I>, which are treated as if they were binary and in two's complement representation. <P>
|
|
The operation to be performed and the return value are determined by <I>op</I>. <P>
|
|
<A REL=DEFINITION HREF="#boole"><B>boole</B></A> returns the values specified for any <I>op</I> in the next figure. <P>
|
|
<P>
|
|
<PRE>
|
|
Op Result
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-1">boole-1</A> integer-1
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-2">boole-2</A> integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-andc1">boole-andc1</A> and complement of integer-1 with integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-andc2">boole-andc2</A> and integer-1 with complement of integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-and">boole-and</A> and
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-c1">boole-c1</A> complement of integer-1
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-c2">boole-c2</A> complement of integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-clr">boole-clr</A> always 0 (all zero bits)
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-eqv">boole-eqv</A> equivalence (exclusive nor)
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-ior">boole-ior</A> inclusive or
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-nand">boole-nand</A> not-and
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-nor">boole-nor</A> not-or
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-orc1">boole-orc1</A> or complement of integer-1 with integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-orc2">boole-orc2</A> or integer-1 with complement of integer-2
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-set">boole-set</A> always -1 (all one bits)
|
|
<A REL=DEFINITION HREF="v_b_1_b.htm#boole-xor">boole-xor</A> exclusive or
|
|
</PRE>
|
|
<P><B>Figure 12-17. Bit-Wise Logical Operations</B> <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(boole boole-ior 1 16) => 17
|
|
(boole boole-and -2 5) => 4
|
|
(boole boole-eqv 17 15) => -31
|
|
|
|
;;; These examples illustrate the result of applying BOOLE and each
|
|
;;; of the possible values of OP to each possible combination of bits.
|
|
(progn
|
|
(format t "~&Results of (BOOLE <op> #b0011 #b0101) ...~
|
|
~%---Op-------Decimal-----Binary----Bits---~%")
|
|
(dolist (symbol '(boole-1 boole-2 boole-and boole-andc1
|
|
boole-andc2 boole-c1 boole-c2 boole-clr
|
|
boole-eqv boole-ior boole-nand boole-nor
|
|
boole-orc1 boole-orc2 boole-set boole-xor))
|
|
(let ((result (boole (symbol-value symbol) #b0011 #b0101)))
|
|
(format t "~& ~A~13T~3,' D~23T~:*~5,' B~31T ...~4,'0B~%"
|
|
symbol result (logand result #b1111)))))
|
|
>> Results of (BOOLE <op> #b0011 #b0101) ...
|
|
>> ---Op-------Decimal-----Binary----Bits---
|
|
>> BOOLE-1 3 11 ...0011
|
|
>> BOOLE-2 5 101 ...0101
|
|
>> BOOLE-AND 1 1 ...0001
|
|
>> BOOLE-ANDC1 4 100 ...0100
|
|
>> BOOLE-ANDC2 2 10 ...0010
|
|
>> BOOLE-C1 -4 -100 ...1100
|
|
>> BOOLE-C2 -6 -110 ...1010
|
|
>> BOOLE-CLR 0 0 ...0000
|
|
>> BOOLE-EQV -7 -111 ...1001
|
|
>> BOOLE-IOR 7 111 ...0111
|
|
>> BOOLE-NAND -2 -10 ...1110
|
|
>> BOOLE-NOR -8 -1000 ...1000
|
|
>> BOOLE-ORC1 -3 -11 ...1101
|
|
>> BOOLE-ORC2 -5 -101 ...1011
|
|
>> BOOLE-SET -1 -1 ...1111
|
|
>> BOOLE-XOR 6 110 ...0110
|
|
=> NIL
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
Should signal <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if its first argument is not a <A REL=DEFINITION HREF="26_glo_b.htm#bit-wise_logical_operation_specifier"><I>bit-wise logical operation specifier</I></A> or if any subsequent argument is not an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_logand.htm#logand"><B>logand</B></A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
In general, <P>
|
|
<PRE>
|
|
(boole boole-and x y) == (logand x y)
|
|
</PRE>
|
|
</TT> <P>
|
|
<A REL=DEFINITION HREF="26_glo_p.htm#programmer"><I>Programmers</I></A> who would prefer to use numeric indices rather than <A REL=DEFINITION HREF="26_glo_b.htm#bit-wise_logical_operation_specifier"><I>bit-wise logical operation specifiers</I></A> can get an equivalent effect by a technique such as the following: <P>
|
|
<PRE>
|
|
;; The order of the values in this `table' are such that
|
|
;; (logand (boole (elt boole-n-vector n) #b0101 #b0011) #b1111) => n
|
|
(defconstant boole-n-vector
|
|
(vector boole-clr boole-and boole-andc1 boole-2
|
|
boole-andc2 boole-1 boole-xor boole-ior
|
|
boole-nor boole-eqv boole-c1 boole-orc1
|
|
boole-c2 boole-orc2 boole-nand boole-set))
|
|
=> BOOLE-N-VECTOR
|
|
(proclaim '(inline boole-n))
|
|
=> <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent">implementation-dependent</A>
|
|
(defun boole-n (n integer &rest more-integers)
|
|
(apply #'boole (elt boole-n-vector n) integer more-integers))
|
|
=> BOOLE-N
|
|
(boole-n #b0111 5 3) => 7
|
|
(boole-n #b0001 5 3) => 1
|
|
(boole-n #b1101 5 3) => -3
|
|
(loop for n from #b0000 to #b1111 collect (boole-n n 5 3))
|
|
=> (0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1)
|
|
</PRE>
|
|
</TT> <P>
|
|
<HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|