Results 1 to 20 of 32
Thread: binary addition and Substraction
 10102011, 03:45 PM #1Member
 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
please help me , i am desperate :XX
 10102011, 04:01 PM #2
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
 10102011, 04:09 PM #3Member
 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.*;
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)));
}
}
 10102011, 04:37 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 10102011, 05:43 PM #5Member
 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());
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;
}
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,
 10102011, 06:06 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
 10112011, 12:43 PM #7Member
 Join Date
 Oct 2011
 Posts
 20
 Rep Power
 0
Re: binary addition and Substraction
can you tell me why? :s
 10112011, 03:00 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
 10122011, 11:45 AM #9Member
 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
 10132011, 06:16 PM #10Member
 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;
 10132011, 06:36 PM #11Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: binary addition and Substraction
It looks to me like the code you posted here is correct.
 10132011, 06:58 PM #12Member
 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
 10132011, 08:11 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
 10142011, 09:33 AM #14Member
 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
 10142011, 09:59 AM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
Re: binary addition and Substraction
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; }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10142011, 07:04 PM #16Member
 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
 10142011, 07:27 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
Re: binary addition and Substraction
Build a wall around Donald Trump; I'll pay for it.
 10142011, 07:39 PM #18Member
 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; 10142011 at 07:43 PM.
 10142011, 07:50 PM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,394
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 10152011, 10:12 AM #20Member
 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;
Similar Threads

addition
By rithish in forum New To JavaReplies: 4Last Post: 10042011, 08:15 AM 
java binary addition
By rockdude in forum New To JavaReplies: 1Last Post: 03182009, 04:01 PM 
Need help on my project on JAVA BINARY ADDITION
By arneltroy in forum New To JavaReplies: 3Last Post: 01312009, 04:33 AM 
Addition of Two time
By jithan in forum New To JavaReplies: 5Last Post: 12292008, 12:09 PM 
Binary Addition
By Deo Favente in forum Advanced JavaReplies: 11Last Post: 04242008, 05:34 AM
Bookmarks