Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008, 04:28 AM
Member
 
Join Date: Feb 2008
Posts: 7
Deo Favente is on a distinguished road
Binary Addition
Ok iv'e been stuck on this for a long time and i still can't get it to work. Binary addition given two arrays of booleans (binary numbers) that must be recursive and return an array of booleans. When i post my code i have so far that usually becomes the focus and everyone gets confused or goes off topic so im not doing it this time. And its binary addition not convert-to-decimal-and-use-jvm-to-add.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-22-2008, 05:51 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Will you post your algorithm on binary addition?

or sight some adding example.....

I know it in assembly, maybe we could formulate that here in java....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-22-2008, 06:17 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,028
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, if you don't put your code here at this point, tell me about the logic/algorithm you workout.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-22-2008, 03:39 PM
Member
 
Join Date: Feb 2008
Posts: 7
Deo Favente is on a distinguished road
This is what iv'e been doing:
1) I found the smaller array, and then made it the same length as the big array
2) I ran each element through a loop to compare the same index in both arrays
3) If both booleans are false, set the index in the final array false
If one is true, set the final boolean true
If both are true, then i make a new number that has 1 true in front followed by all false, so that the true is one ahead of the current index, thenm i add the temporary array to the final array (this is generally where i run into problems)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-23-2008, 04:46 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,028
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
From the beginning I'm stuck.

Quote:
Originally Posted by Deo Favente View Post
1) I found the smaller array, and then made it the same length as the big array
What you mean smaller array and big array. Since you use the word 'same length' I can't figure out what is the different of smaller and big. It's better to make it more clear.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-23-2008, 05:11 AM
Member
 
Join Date: Feb 2008
Posts: 7
Deo Favente is on a distinguished road
well the two arrays most likey arn't going to be the same size, and if you try to run a for loop going thru each loop at the same time ur gonna get a out of bounds error so i need to continue to prepend the arrays so that they both align up, like how we do addition. When we do addition and the numbers arn't the same length we know that there's as many zeros in fron of the shorter number as we need there to be.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-23-2008, 05:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,028
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, I got the point. What you have done up to now.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-23-2008, 05:48 AM
Member
 
Join Date: Feb 2008
Posts: 7
Deo Favente is on a distinguished road
everything except the last part of 3
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-23-2008, 06:06 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,028
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
After comparing two arrays with the same index, you can find the result, that both are false, only one or both are true. Is that right?

Next, can you refer the same index of the final array? If so why can't you refer the previous index?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-23-2008, 06:39 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
Hi Deo, here is your algorithm if I understood the problem

Code:
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))); } }
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-24-2008, 06:13 AM
Member
 
Join Date: Feb 2008
Posts: 7
Deo Favente is on a distinguished road
ok thanks that works well. I'm actually using a class that contains a boolean array for my implementation but it fits right in. I see now where i went wrong, and you have also provided a more efficent way of aligning the arrays to do the addition. Thanks a bunch
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-24-2008, 06:34 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
You are welcome. It was fun to implement
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Addition program that displays the sum of two numbers Java Tip Java Tips 0 03-28-2008 09:46 PM
jTextField Simpe Addition - how to ? - Netbeans 6.0 jesicapalma NetBeans 2 03-25-2008 12:07 PM
binary search tranceluv New To Java 10 01-14-2008 08:13 PM
Varargs – Java 5.0 addition JavaForums Java Blogs 0 11-13-2007 05:40 PM
Binbot Binary Newsreader 1.1b2 JavaBean Java Announcements 0 06-28-2007 02:27 PM


All times are GMT +3. The time now is 08:24 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org