Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > boolean logic

Reply
Thread Tools

boolean logic

 
 
Miss. Michelle Heigardt
Guest
Posts: n/a
 
      01-15-2004
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
 
Reply With Quote
 
 
 
 
Jos A. Horsmeier
Guest
Posts: n/a
 
      01-15-2004
"Miss. Michelle Heigardt" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> 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
 
Reply With Quote
 
 
 
 
Erstwhile
Guest
Posts: n/a
 
      01-16-2004
Jos A. Horsmeier wrote:
> "Miss. Michelle Heigardt" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
>
>>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

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

 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      01-16-2004
Miss. Michelle Heigardt wrote:

> ...(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);
}
</code>

> 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.
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      01-17-2004
"Erstwhile" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jos A. Horsmeier wrote:
> > "Miss. Michelle Heigardt" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed) om...
> >
> >>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

> 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.


 
Reply With Quote
 
Miss. Michelle Heigardt
Guest
Posts: n/a
 
      01-18-2004
Thomas Schodt <"news04jan"@\"xenoc.demon.co.uk\"> wrote in message news:<bu91jd$b7r$1$(E-Mail Removed)>...


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



Lots of XOR of each number in set. Yes. Thank you. Michelle
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      01-19-2004
Thomas Schodt wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synthesis of Logic on Non-boolean Constants rickman VHDL 9 04-02-2011 05:17 AM
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
boolean logic question Kurt Krueckeberg C++ 1 09-23-2004 05:35 PM
Clarity of boolean logic Christopher Benson-Manica C Programming 7 12-30-2003 06:05 PM



Advertisments