boolean logic

Discussion in 'Java' started by Miss. Michelle Heigardt, Jan 15, 2004.

  1. 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
    Miss. Michelle Heigardt, Jan 15, 2004
    #1
    1. Advertising

  2. "Miss. Michelle Heigardt" <> wrote in message news:...
    > 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
    Jos A. Horsmeier, Jan 15, 2004
    #2
    1. Advertising

  3. Miss. Michelle Heigardt

    Erstwhile Guest

    Jos A. Horsmeier wrote:
    > "Miss. Michelle Heigardt" <> wrote in message news:...
    >
    >>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
    Erstwhile, Jan 16, 2004
    #3
  4. 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.
    Thomas Schodt, Jan 16, 2004
    #4
  5. Miss. Michelle Heigardt

    xarax Guest

    "Erstwhile" <> wrote in message
    news:...
    > Jos A. Horsmeier wrote:
    > > "Miss. Michelle Heigardt" <> wrote in message

    news:...
    > >
    > >>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.
    xarax, Jan 17, 2004
    #5
  6. Thomas Schodt <"news04jan"@\"xenoc.demon.co.uk\"> wrote in message news:<bu91jd$b7r$1$>...


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



    Lots of XOR of each number in set. Yes. Thank you. Michelle
    Miss. Michelle Heigardt, Jan 18, 2004
    #6
  7. 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
    Thomas Weidenfeller, Jan 19, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg

    boolean logic question

    Kurt Krueckeberg, Sep 23, 2004, in forum: C++
    Replies:
    1
    Views:
    375
    Simon Stienen
    Sep 23, 2004
  2. Christopher Benson-Manica

    Clarity of boolean logic

    Christopher Benson-Manica, Dec 23, 2003, in forum: C Programming
    Replies:
    7
    Views:
    380
    Christopher Benson-Manica
    Dec 30, 2003
  3. J Leonard
    Replies:
    4
    Views:
    12,663
    Mark Space
    Jan 19, 2008
  4. rickman
    Replies:
    9
    Views:
    824
    rickman
    Apr 2, 2011
  5. Metre Meter
    Replies:
    7
    Views:
    363
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page