01: /*
02: * logand.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: logand.java,v 1.6 2003/11/15 11:03:31 beedlem Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: import java.math.BigInteger;
25:
26: // ### logand
27: // logand &rest integers => result-integer
28: public final class logand extends Primitive {
29: private logand() {
30: super ("logand");
31: }
32:
33: public LispObject execute() {
34: return Fixnum.MINUS_ONE;
35: }
36:
37: public LispObject execute(LispObject first, LispObject second)
38: throws ConditionThrowable {
39: if (first instanceof Fixnum && second instanceof Fixnum) {
40: return new Fixnum(((Fixnum) first).getValue()
41: & ((Fixnum) second).getValue());
42: } else {
43: BigInteger n1, n2;
44: if (first instanceof Fixnum)
45: n1 = ((Fixnum) first).getBigInteger();
46: else if (first instanceof Bignum)
47: n1 = ((Bignum) first).getValue();
48: else
49: throw new ConditionThrowable(new TypeError(first,
50: "integer"));
51: if (second instanceof Fixnum)
52: n2 = ((Fixnum) second).getBigInteger();
53: else if (second instanceof Bignum)
54: n2 = ((Bignum) second).getValue();
55: else
56: throw new ConditionThrowable(new TypeError(second,
57: "integer"));
58: return number(n1.and(n2));
59: }
60: }
61:
62: public LispObject execute(LispObject[] args)
63: throws ConditionThrowable {
64: BigInteger result = BigInteger.valueOf(-1);
65: for (int i = 0; i < args.length; i++) {
66: BigInteger n;
67: if (args[i] instanceof Fixnum)
68: n = ((Fixnum) args[i]).getBigInteger();
69: else if (args[i] instanceof Bignum)
70: n = ((Bignum) args[i]).getValue();
71: else
72: throw new ConditionThrowable(new TypeError(args[i],
73: "integer"));
74: result = result.and(n);
75: }
76: return number(result);
77: }
78:
79: private static final logand LOGAND = new logand();
80: }
|