01: /*
02: * logand.java
03: *
04: * Copyright (C) 2003-2004 Peter Graves
05: * $Id: logand.java,v 1.10 2004/03/04 01:26:58 piso 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", "&rest integers");
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).value
41: & ((Fixnum) second).value);
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).value;
48: else
49: return signal(new TypeError(first, Symbol.INTEGER));
50: if (second instanceof Fixnum)
51: n2 = ((Fixnum) second).getBigInteger();
52: else if (second instanceof Bignum)
53: n2 = ((Bignum) second).value;
54: else
55: return signal(new TypeError(second, Symbol.INTEGER));
56: return number(n1.and(n2));
57: }
58: }
59:
60: public LispObject execute(LispObject[] args)
61: throws ConditionThrowable {
62: BigInteger result = BigInteger.valueOf(-1);
63: for (int i = 0; i < args.length; i++) {
64: BigInteger n;
65: if (args[i] instanceof Fixnum)
66: n = ((Fixnum) args[i]).getBigInteger();
67: else if (args[i] instanceof Bignum)
68: n = ((Bignum) args[i]).value;
69: else
70: return signal(new TypeError(args[i], Symbol.INTEGER));
71: result = result.and(n);
72: }
73: return number(result);
74: }
75:
76: private static final Primitive LOGAND = new logand();
77: }
|