Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default binary addition and Substraction

    hi everybody :)

    i am Michael, and i got a big problem and i hope i can get some help :x

    i got a school work, to make a simple binary addition and Substraction, with netbeans .


    i know how binary works, but i cant do it to java language :x

    0 + 0 = 0
    0 + 1 = 1
    1 + 0 = 1
    1 + 1 = 1 and transported

    please help me , i am desperate :XX

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: binary addition and Substraction

    Moved from 'Advanced Java'. Please don't post homework in that forum.

    Also, nobody here will do your homework for you. Post your best efforts and indicate where you still have a problem, and you're sure to get good help.

    db

  3. #3
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    ok, but is this the solution for my problem?
    package test;

    import java.util.*;

    public class BinaryAdd
    {
    private static boolean [] add(boolean [] a, boolean [] b)
    {
    // pad them to same length
    int l = a.length > b.length ? a.length : b.length;
    a = Arrays.copyOf(a, l);
    b = Arrays.copyOf(b, l);

    // prevent overflow, make the results bigger
    boolean [] r = new boolean[a.length + 1];
    boolean t = false;

    for (int i = 0; i < l; i++)
    {
    if (a[i] && b[i])
    {
    if (t)
    {
    r[i] = true;
    t = true;
    }
    else
    {
    r[i] = false;
    t = true;
    }
    }
    else if (a[i] || b[i])
    {
    if (t)
    {
    r[i] = false;
    t = true;
    }
    else
    {
    r[i] = true;
    t = false;
    }
    }
    else
    {
    if (t)
    {
    r[i] = true;
    t = false;
    }
    else
    {
    r[i] = false;
    t = false;
    }
    }
    }

    // check overflow
    if (t)
    {
    r[l] = true;
    }

    return r;
    }

    public static void main(String[] args)
    {
    // least significant position is at 0
    boolean a1[] = {true};
    boolean a2[] = {false};
    boolean a3[] = {true, true};
    boolean a4[] = {false, false};
    boolean a5[] = {true, false};
    boolean a6[] = {false, true};

    boolean a7[] = {true, true, true, true};
    boolean a8[] = {false, true, false, true};

    // keep in mind least significant value is at index 0
    System.out.println(Arrays.toString(a1) + " + " + Arrays.toString(a1) + " = " + Arrays.toString(add(a1, a1)));
    System.out.println(Arrays.toString(a1) + " + " + Arrays.toString(a2) + " = " + Arrays.toString(add(a1, a2)));
    System.out.println(Arrays.toString(a3) + " + " + Arrays.toString(a3) + " = " + Arrays.toString(add(a3, a3)));
    System.out.println(Arrays.toString(a7) + " + " + Arrays.toString(a8) + " = " + Arrays.toString(add(a7, a8)));
    }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    From what I remember from the old days: given input a, b and c (c is the carry bit) and outputs s and d (s is the sum of a and b, and d is the new carry bit), the functional specification of a full adder is: s=a^b^c, d = a&b | a&c | b&c

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    i got a new code,

    static String [] lookupTable = {
    "0+0+0=00",
    "0+0+1=01",
    "0+1+0=01",
    "0+1+1=10",
    "1+0+0=01",
    "1+0+1=10",
    "1+1+0=10",
    "1+1+1=11",
    };
    static String lookup(char s1, char s2, char c) {
    String formula = String.format("%c+%c+%c=", s1, s2, c);
    for (String s : lookupTable) {
    if (s.startsWith(formula)) {
    return s.substring(s.indexOf("=") + 1);
    }
    }
    throw new IllegalArgumentException();
    }
    static String zeroPad(String s, int length) {
    while (s.length() < length) {
    s = "0" + s;
    }
    return s;
    }
    static String add(String s1, String s2) {
    int length = Math.max(s1.length(), s2.length());
    s1 = zeroPad(s1, length);
    s2 = zeroPad(s2, length);
    String result = "";
    char carry = '0';
    for (int i = length - 1; i >= 0; i--) {
    String columnResult = lookup(s1.charAt(i), s2.charAt(i), carry);
    result = columnResult.charAt(1) + result;
    carry = columnResult.charAt(0);
    }
    if (carry == '1') {
    result = carry + result;
    }
    return result;
    }
    then i got in a botton

    String s1,s2;

    s1 = (jTextField1.getText());
    s2 = (jTextField2.getText());


    jLabel1.setText(add(s1,s2));

    and in another
    jLabel1.setText(" ");
    jTextField1.setText(" ");
    jTextField2.setText(" ");

    and it works, but i got some issues still, like , i just can do the operation one time,

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    i got a new code,
    That code is so silly; it''s not worth discussing it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    can you tell me why? :s

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    can you tell me why? :s
    Yes, it is silly to use a lookup table for something simple as a full adder (see my first reply) and particularly when done through human readable Strings.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    i tried to put this code that is in pascal to java , but i got some problems in the construction of strings
    Re: PROGRAMA EM PASCAL PARA SOMAR DOIS NUMEROS BIN - PCForum.com.br

    can you help , in the code ,to contruct the

    0 + 0 = 0
    0 + 1 = 1
    1 + 0 = 1
    1 + 1 = 1 and transported

    that i just cant, i got a lot of errors and i dont understand :s

  10. #10
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    got to be something like this?
    Java Code:
    jTextField1.getText();
    jTextField2.getText();
    vem=vai;
    while (jTextField1.getText().isEmpty()) 
    {
    bin1=Integer.parseInt(jTextField1.getText());
    bin2=Integer.parseInt(jTextField2.getText());
    }
    
    
    if ((vem==0)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=0;        
    }
    if ((vem==0)&(alg1==1)&(alg2==0))      |     ((vem==0)&(alg1==0)&(alg2==1))
            {
    vai=0;
    somabin=1;
            }
    if ((vem==0)&(alg1==1)&(alg2==1))
            {
          vai=1;
          somabin=0;
    }
    if ((vem==1)&(alg1==0)&(alg2==0))
     {
            vai=0;
    somabin=1;
     }
     if ((vem==1) & (alg1==1) & (alg2==0)) | ((vem==1) & (alg1==0) & (alg2==1)) 
             {
    vai=1;
    somabin=0;
             }
    if ((vem==1)&(alg1==1)&(alg2==1)) 
    {
    vai=1;
    somabin=1;
    }
    result=String.valueOf(somabin);
    result_final=result+result_final;

  11. #11
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: binary addition and Substraction

    It looks to me like the code you posted here is correct.

  12. #12
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    its not correct because i got in the print 0null :s

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    got to be something like this?
    Oh dear, is my reply #4 written in invisible ink again?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    and i didn't do that?
    can you help me in code with you said ? :d

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    and i didn't do that?
    can you help me in code with you said ? :d
    Ok, let a and b be two boolean arrays; the booleans represent the bits (true == 1, false == 0); assume that both a and b have equal lengths. The following method takes the arrays a, b and c; c is an array where the 'sum' of a and b is stored; here goes:

    Java Code:
    public boolean sum(boolean[] a, boolean[] b, boolean[] c) {
       boolean carry= false;
       for (int i= 0; i < a.length; i++) {
          c[i]= a[i]^b[i]^carry;
          carry= a[i]&b[i] | a[i]&carry | b[i]&carry;
       }
       return carry;
    }
    Observe that the last carry bit is returned from the method; do with it what you want.If you get a good grade you should hand it over to me ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    and that is for the first code i show? :s, men, i only started java 1 week and half ago, i am a litle noob :ds

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    and that is for the first code i show? :s, men, i only started java 1 week and half ago, i am a litle noob :ds
    No, you shouldn't show it to anyone because they'd recognize that you had just bluntly copied and pasted it. Try to make a runnable little test class from it and try to understand what it's doing and craft your own code out of it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    let me see if I understand.
    This code serves as a function to transport (carry), right?
    only to have to get the input of the user, read input, like this

    jTextField1.getText ();
    jTextField2.getText ();

    then I have to do this to serve as a transport, and that's where it enters the carry?

    0 + 0 = 0
    0 + 1 = 1
    1 + 1 = 0
    1 + 1 = 1 and Transported

    because i am bulding a java application to make + and - operation, with 2 input numbers like"1100" and "1000", and give the result :S

    and i can copy a program from internet, but i only have to do is defend my project so i have to understand it :)
    Last edited by m1ke4fun; 10-14-2011 at 07:43 PM.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Forget about reading those bits for now; make the little method I supplied run. That carry thing is mostly internal to the method, it only returns the final carry value to the caller; you're free to do with it what you want. Once you have it running, you can worry about the frillies of reading numbers from text fields etc. First make your logic run.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    hum, ok, but i prefer to do a more basic application, without creating classes, i got already a code , that i did, but, actually, it returns in printf "0null", i know it got errors, most problably in the contrution of strings, but can you see, if got any error stupid?
    jTextField1.getText();
    jTextField2.getText();
    vem=vai;
    while (jTextField1.getText().isEmpty())
    {
    bin1=Integer.parseInt(jTextField1.getText());
    bin2=Integer.parseInt(jTextField2.getText());
    }


    if ((vem==0)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=0;
    }
    if ((vem==0)&(alg1==1)&(alg2==0)) | ((vem==0)&(alg1==0)&(alg2==1))
    {
    vai=0;
    somabin=1;
    }
    if ((vem==0)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=0;
    }
    if ((vem==1)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=1;
    }
    if ((vem==1) & (alg1==1) & (alg2==0)) | ((vem==1) & (alg1==0) & (alg2==1))
    {
    vai=1;
    somabin=0;
    }
    if ((vem==1)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=1;
    }
    result=String.valueOf(somabin);
    result_final=result+result_final;

Page 1 of 2 12 LastLast

Similar Threads

  1. addition
    By rithish in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 08:15 AM
  2. java binary addition
    By rockdude in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:01 PM
  3. Need help on my project on JAVA BINARY ADDITION
    By arneltroy in forum New To Java
    Replies: 3
    Last Post: 01-31-2009, 03:33 AM
  4. Addition of Two time
    By jithan in forum New To Java
    Replies: 5
    Last Post: 12-29-2008, 11:09 AM
  5. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •