boolean logic

  • Thread starter Miss. Michelle Heigardt
  • Start date
M

Miss. Michelle Heigardt

Hallo, I have a simple question where I want to comment my Java code.

I have rule below

//I want to put comment in here to describe the boolean
//test the method does.
private int booleanRule(int i1, int i2, int i3, int i4, int i5)
throws Exception
{
if (!((i1==0) | (i1==1))) throw new Exception("Illegal i1");
if (!((i2==0) | (i2==1))) throw new Exception("Illegal i2");
if (!((i3==0) | (i3==1))) throw new Exception("Illegal i3");
if (!((i4==0) | (i4==1))) throw new Exception("Illegal i4");
if (!((i5==0) | (i5==1))) throw new Exception("Illegal i5");
if ((i1+i2+i3+i4+i5)>1) throw new Exception("Allowed only 1 i
set");
return (i1+i2+i3+i4+i5);
}

For the comments in my code what can I describe this as? (I now it is
not a NAND or a NOR or an AND or an OR or a BIT).

What it does is that if all i1, i2, i3, i4, and i5 is 0, it returns 0.
If any of i1, i2, i3, i4, i5 is 1 it returns 1.
If more than 1 i is set, it errors.
If i is anything other than 1 or 0 it errors.

Thank you.
Michelle
 
J

Jos A. Horsmeier

Miss. Michelle Heigardt said:
Hallo, I have a simple question where I want to comment my Java code.

I have rule below

//I want to put comment in here to describe the boolean
//test the method does.
private int booleanRule(int i1, int i2, int i3, int i4, int i5)
throws Exception
{
if (!((i1==0) | (i1==1))) throw new Exception("Illegal i1");
if (!((i2==0) | (i2==1))) throw new Exception("Illegal i2");
if (!((i3==0) | (i3==1))) throw new Exception("Illegal i3");
if (!((i4==0) | (i4==1))) throw new Exception("Illegal i4");
if (!((i5==0) | (i5==1))) throw new Exception("Illegal i5");
if ((i1+i2+i3+i4+i5)>1) throw new Exception("Allowed only 1 i
set");
return (i1+i2+i3+i4+i5);
}

For the comments in my code what can I describe this as? (I now it is
not a NAND or a NOR or an AND or an OR or a BIT).

What it does is that if all i1, i2, i3, i4, and i5 is 0, it returns 0.
If any of i1, i2, i3, i4, i5 is 1 it returns 1.
If more than 1 i is set, it errors.
If i is anything other than 1 or 0 it errors.

IMHO, your last paragraph, starting with "if all i1, i2 ..." serves
perfectly well as a comment paragraph. If you don't need the verbosity
of the code, it could be contracted to --

if (((i1|i2|i3|i4|i5)&~1) != 0) throw new Exception("Illegal i value");
if (i1+i2+i3+i4+i5 > 1) throw new Exception("Allowed only 1 1");

kind regards,

Jos
 
E

Erstwhile

Jos said:
IMHO, your last paragraph, starting with "if all i1, i2 ..." serves
perfectly well as a comment paragraph. If you don't need the verbosity
of the code, it could be contracted to --

if (((i1|i2|i3|i4|i5)&~1) != 0) throw new Exception("Illegal i value");
if (i1+i2+i3+i4+i5 > 1) throw new Exception("Allowed only 1 1");

kind regards,

Jos
Actually the above code is wrong ... consider i1=-1, i2=1, i3=-2,i4=2, i5=1
 
T

Thomas Schodt

Miss. Michelle Heigardt said:
...(code)...

I'd probably do

<code>
private boolean toBoolean(int i,String name) throws Exception {
if (i==0) return false;
if (i==1) return true;
throw new Exception("Illegal "+name);
}

// for compatibility
private int booleanRule(int i1, int i2, int i3, int i4, int i5)
throws Exception
{
int[] i = new int[5];
i[0] = i1; i[1] = i2; i[2] = i3; i[3] = i4; i[4] = i5;
return booleanRule(i);
}

// more flexible
private int booleanRule(int i[]) throws Exception {
int n = 0;
for(int j=0;j<i.length;++j) {
if (toBoolean(i[j],"i["+j+"]")) ++n;
}
return (toBoolean(n,"set")?1:0);
}
For the comments in my code what can I describe this as? (I know it is
not a NAND or a NOR or an AND or an OR or a BIT).

The logic has something in common with exclusive-or.
 
X

xarax

Erstwhile said:
Actually the above code is wrong ... consider i1=-1, i2=1, i3=-2,i4=2, i5=1

That case is filtered out by the first if(), which tests
the bits using &~1. ~1 == 0xfffffffe. If it gets past
the first if(), then it knows that values are all either
0 or 1, nothing else. The second if() tests for whether
there is more than one value 1.
 
T

Thomas Weidenfeller

Thomas said:
private boolean toBoolean(int i,String name) throws Exception {
if (i==0) return false;
if (i==1) return true;
throw new Exception("Illegal "+name);
}

The original problem has been solved in another way, but I always get
nervous if I see such kind of code. Use something like

return i != 0;

instead. Forget about the 'if' and the exception. Especially the generic
Exception.
int[] i = new int[5];
i[0] = i1; i[1] = i2; i[2] = i3; i[3] = i4; i[4] = i5;

int i[] = { i1, i2, i3, i4, i5 };
private int booleanRule(int i[]) throws Exception {
int n = 0;
for(int j=0;j<i.length;++j) {
if (toBoolean(i[j],"i["+j+"]")) ++n;
}
return (toBoolean(n,"set")?1:0);
}

Depends on what you want to really do with your exceptions (you would
get one if more than one element in i[] is != 0). I would start with
something like:

int n = 0;
for(int j = 0; j < i.length; j++) {
n |= i[j];
}
return n;

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top