1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/f_boole.htm
2024-04-01 10:24:07 +02:00

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> =&gt; <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) =&gt; 17
(boole boole-and -2 5) =&gt; 4
(boole boole-eqv 17 15) =&gt; -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 &quot;~&amp;Results of (BOOLE &lt;op&gt; #b0011 #b0101) ...~
~%---Op-------Decimal-----Binary----Bits---~%&quot;)
(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 &quot;~&amp; ~A~13T~3,' D~23T~:*~5,' B~31T ...~4,'0B~%&quot;
symbol result (logand result #b1111)))))
&gt;&gt; Results of (BOOLE &lt;op&gt; #b0011 #b0101) ...
&gt;&gt; ---Op-------Decimal-----Binary----Bits---
&gt;&gt; BOOLE-1 3 11 ...0011
&gt;&gt; BOOLE-2 5 101 ...0101
&gt;&gt; BOOLE-AND 1 1 ...0001
&gt;&gt; BOOLE-ANDC1 4 100 ...0100
&gt;&gt; BOOLE-ANDC2 2 10 ...0010
&gt;&gt; BOOLE-C1 -4 -100 ...1100
&gt;&gt; BOOLE-C2 -6 -110 ...1010
&gt;&gt; BOOLE-CLR 0 0 ...0000
&gt;&gt; BOOLE-EQV -7 -111 ...1001
&gt;&gt; BOOLE-IOR 7 111 ...0111
&gt;&gt; BOOLE-NAND -2 -10 ...1110
&gt;&gt; BOOLE-NOR -8 -1000 ...1000
&gt;&gt; BOOLE-ORC1 -3 -11 ...1101
&gt;&gt; BOOLE-ORC2 -5 -101 ...1011
&gt;&gt; BOOLE-SET -1 -1 ...1111
&gt;&gt; BOOLE-XOR 6 110 ...0110
=&gt; 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) =&gt; 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))
=&gt; BOOLE-N-VECTOR
(proclaim '(inline boole-n))
=&gt; <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent">implementation-dependent</A>
(defun boole-n (n integer &amp;rest more-integers)
(apply #'boole (elt boole-n-vector n) integer more-integers))
=&gt; BOOLE-N
(boole-n #b0111 5 3) =&gt; 7
(boole-n #b0001 5 3) =&gt; 1
(boole-n #b1101 5 3) =&gt; -3
(loop for n from #b0000 to #b1111 collect (boole-n n 5 3))
=&gt; (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>