1. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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  Reply With Quote

2. ## Re: binary addition and Substraction

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  Reply With Quote

3. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## Re: binary addition and Substraction

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

import java.util.*;

{
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)));
}
}  Reply With Quote

4. ## 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  Reply With Quote

5. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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());
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());

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,  Reply With Quote

6. ## Re: binary addition and Substraction Originally Posted by m1ke4fun i got a new code,
That code is so silly; it''s not worth discussing it.

kind regards,

Jos  Reply With Quote

7. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## Re: binary addition and Substraction

can you tell me why? :s  Reply With Quote

8. ## Re: binary addition and Substraction Originally Posted by m1ke4fun 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  Reply With Quote

9. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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  Reply With Quote

10. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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;```  Reply With Quote

11. Member Join Date
Oct 2011
Posts
81
Rep Power
0

## Re: binary addition and Substraction

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

12. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## Re: binary addition and Substraction

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

13. ## Re: binary addition and Substraction Originally Posted by m1ke4fun got to be something like this?
Oh dear, is my reply #4 written in invisible ink again?

kind regards,

Jos  Reply With Quote

14. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## Re: binary addition and Substraction

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

15. ## Re: binary addition and Substraction Originally Posted by m1ke4fun 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  Reply With Quote

16. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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  Reply With Quote

17. ## Re: binary addition and Substraction Originally Posted by m1ke4fun 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  Reply With Quote

18. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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 08:43 PM.  Reply With Quote

19. ## 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  Reply With Quote

20. Member Join Date
Oct 2011
Posts
20
Rep Power
0

## 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;  Reply With Quote

#### Posting Permissions

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