1
0
Fork 0
cl-sites/novaspec.org/cl/f_boole.html

670 lines
15 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>boole | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_boole.html"
><LINK REL="next" HREF="v_boole-1.html" TYPE="text/html" TITLE="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"
><LINK REL="prev" HREF="f_parse-integer.html" TYPE="text/html" TITLE="parse-integer"
><LINK REL="up" HREF="12_2_Numbers_Dictionary.html" TYPE="text/html" TITLE="12.2 Numbers Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="12_Numbers.html"
>12. Numbers</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="12_2_Numbers_Dictionary.html"
>12.2 Numbers Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_boole.html"
>boole</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_parse-integer.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="v_boole-1.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="boole"
></SPAN
><B
>boole</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>boole</B
> <SPAN CLASS="cmssi"
>op</SPAN
> <SPAN CLASS="cmssi"
>integer-1</SPAN
> <SPAN CLASS="cmssi"
>integer-2</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>result-integer</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>Op</VAR
> &#8212; a <A HREF="26_1_Glossary.html#bit-wise_logical_operation_specifier"
><EM CLASS="term"
>bit-wise logical operation specifier</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>integer-1</VAR
> &#8212; an <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>integer-2</VAR
> &#8212; an <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>result-integer</VAR
> &#8212; an <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_boole.html" CLASS="funref"
><B
>boole</B
></A
> performs bit-wise logical operations on <VAR CLASS="param"
>integer-1</VAR
> and <VAR CLASS="param"
>integer-2</VAR
>, which are treated as if they were binary and in two&#8217;s complement representation. </P
><P CLASS="j"
>The operation to be performed and the return value are determined by <VAR CLASS="param"
>op</VAR
>. </P
><P CLASS="j"
><A HREF="f_boole.html" CLASS="funref"
><B
>boole</B
></A
> returns the values specified for any <VAR CLASS="param"
>op</VAR
> in Figure 12&#8211;17.</P
><FIGURE CLASS="boxfig"
><DIV CLASS="figbody"
><TABLE CELLSPACING="0" CELLPADDING="0" RULES="GROUPS" STYLE="margin: 1ex 0" WIDTH="100%"
><THEAD
><TR
><TH ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
><B
>Op</B
></TH
><TH ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <B
>Result</B
></TH
></TR
></THEAD
><TBODY
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-1</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <VAR CLASS="param"
>integer-1</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-2</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-andc1</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-1</VAR
> <SPAN CLASS="cmr"
>with</SPAN
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-andc2</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>and</SPAN
> <VAR CLASS="param"
>integer-1</VAR
> <SPAN CLASS="cmr"
>with</SPAN
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-and</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>and</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-c1</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-1</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-c2</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-clr</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>always</SPAN
> <SPAN CLASS="cmr"
>0</SPAN
> <SPAN CLASS="cmr"
>(all</SPAN
> <SPAN CLASS="cmr"
>zero</SPAN
> <SPAN CLASS="cmr"
>bits)</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-eqv</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>equivalence</SPAN
> <SPAN CLASS="cmr"
>(exclusive</SPAN
> <SPAN CLASS="cmr"
>nor)</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-ior</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>inclusive</SPAN
> <SPAN CLASS="cmr"
>or</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-nand</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>not-and</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-nor</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>not-or</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-orc1</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>or</SPAN
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-1</VAR
> <SPAN CLASS="cmr"
>with</SPAN
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-orc2</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>or</SPAN
> <VAR CLASS="param"
>integer-1</VAR
> <SPAN CLASS="cmr"
>with</SPAN
> <SPAN CLASS="cmr"
>complement</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <VAR CLASS="param"
>integer-2</VAR
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-set</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>always</SPAN
> <SPAN CLASS="cmr"
>-1</SPAN
> <SPAN CLASS="cmr"
>(all</SPAN
> <SPAN CLASS="cmr"
>one</SPAN
> <SPAN CLASS="cmr"
>bits)</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
> <A HREF="v_boole-1.html" CLASS="conref"
><B
>boole-xor</B
></A
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>exclusive</SPAN
> <SPAN CLASS="cmr"
>or</SPAN
></TD
></TR
></TBODY
></TABLE
></DIV
><FIGCAPTION CLASS="caption"
><B
>Figure 12&#8211;17. Bit-Wise Logical Operations</B
></FIGCAPTION
></FIGURE
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (boole boole-ior 1 16) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 17
(boole boole-and -2 5) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 4
(boole boole-eqv 17 15) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> -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 "~&amp;Results of (BOOLE &lt;op&gt; #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 "~&amp; ~A~13T~3,' D~23T~:*~5,' B~31T ...~4,'0B~%"
symbol result (logand result #b1111)))))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Results of (BOOLE &lt;op&gt; #b0011 #b0101) ...
<SPAN CLASS="cmr"
>&#8883;</SPAN
> ---Op-------Decimal-----Binary----Bits---
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-1 3 11 ...0011
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-2 5 101 ...0101
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-AND 1 1 ...0001
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-ANDC1 4 100 ...0100
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-ANDC2 2 10 ...0010
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-C1 -4 -100 ...1100
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-C2 -6 -110 ...1010
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-CLR 0 0 ...0000
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-EQV -7 -111 ...1001
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-IOR 7 111 ...0111
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-NAND -2 -10 ...1110
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-NOR -8 -1000 ...1000
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-ORC1 -3 -11 ...1101
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-ORC2 -5 -101 ...1011
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-SET -1 -1 ...1111
<SPAN CLASS="cmr"
>&#8883;</SPAN
> BOOLE-XOR 6 110 ...0110
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>Should signal <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> if its first argument is not a <A HREF="26_1_Glossary.html#bit-wise_logical_operation_specifier"
><EM CLASS="term"
>bit-wise logical operation specifier</EM
></A
> or if any subsequent argument is not an <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_logand.html" CLASS="funref"
><B
>logand</B
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>In general,</P
><PRE CLASS="screen"
>(boole boole-and x y) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> (logand x y)</PRE
><P CLASS="j"
><A HREF="26_1_Glossary.html#programmer"
><EM CLASS="term"
>Programmers</EM
></A
> who would prefer to use numeric indices rather than <A HREF="26_1_Glossary.html#bit-wise_logical_operation_specifier"
><EM CLASS="term"
>bit-wise logical operation specifiers</EM
></A
> can get an equivalent effect by a technique such as the following:</P
><PRE CLASS="screen"
>;; 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))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> BOOLE-N-VECTOR
(proclaim '(inline boole-n))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
>
(defun boole-n (n integer &amp;rest more-integers)
(apply #'boole (elt boole-n-vector n) integer more-integers))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> BOOLE-N
(boole-n #b0111 5 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 7
(boole-n #b0001 5 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(boole-n #b1101 5 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> -3
(loop for n from #b0000 to #b1111 collect (boole-n n 5 3))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1)</PRE
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_parse-integer.html" CLASS="prev"
>&#8592;</A
><A HREF="v_boole-1.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>