KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > logxor


1 /*
2  * logxor.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: logxor.java,v 1.5 2004/02/28 17:44:46 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25
26 // ### logxor
27
// logxor &rest integers => result-integer
28
// exclusive or
29
public final class logxor extends Primitive
30 {
31     private logxor()
32     {
33         super("logxor", "&rest integers");
34     }
35
36     public LispObject execute()
37     {
38         return Fixnum.ZERO;
39     }
40
41     public LispObject execute(LispObject arg) throws ConditionThrowable
42     {
43         if (arg instanceof Fixnum || arg instanceof Bignum)
44             return arg;
45         return signal(new TypeError(arg, Symbol.INTEGER));
46     }
47
48     public LispObject execute(LispObject first, LispObject second)
49         throws ConditionThrowable
50     {
51         if (first instanceof Fixnum && second instanceof Fixnum)
52             return new Fixnum(((Fixnum)first).value ^ ((Fixnum)second).value);
53         BigInteger JavaDoc n1, n2;
54         if (first instanceof Fixnum)
55             n1 = ((Fixnum)first).getBigInteger();
56         else if (first instanceof Bignum)
57             n1 = ((Bignum)first).value;
58         else
59             return signal(new TypeError(first, Symbol.INTEGER));
60         if (second instanceof Fixnum)
61             n2 = ((Fixnum)second).getBigInteger();
62         else if (second instanceof Bignum)
63             n2 = ((Bignum)second).value;
64         else
65             return signal(new TypeError(second, Symbol.INTEGER));
66         return number(n1.xor(n2));
67     }
68
69     public LispObject execute(LispObject[] args) throws ConditionThrowable
70     {
71         final int limit = args.length;
72         int i = 0;
73         // Maybe all the arguments are fixnums.
74
int r = 0;
75         do {
76             if (args[i] instanceof Fixnum) {
77                 r ^= ((Fixnum)args[i]).value;
78                 ++i;
79             } else
80                 break;
81         } while (i < limit);
82         if (i == limit)
83             return number(r);
84         // Not all fixnums.
85
BigInteger JavaDoc result = BigInteger.valueOf(r);
86         while (i < limit) {
87             BigInteger JavaDoc n;
88             if (args[i] instanceof Fixnum)
89                 n = ((Fixnum)args[i]).getBigInteger();
90             else if (args[i] instanceof Bignum)
91                 n = ((Bignum)args[i]).value;
92             else
93                 return signal(new TypeError(args[i], Symbol.INTEGER));
94             result = result.xor(n);
95             ++i;
96         }
97         return number(result);
98     }
99
100     private static final Primitive LOGXOR = new logxor();
101 }
102
Popular Tags