Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Binary Addition With Boolean Array
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

 Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-10-2011, 03:08 AM PM User | #1 Jaxyral New Coder   Join Date: Apr 2010 Posts: 16 Thanks: 2 Thanked 0 Times in 0 Posts Binary Addition With Boolean Array I have about 6 different projects due tomorrow and have been programming all week. The code for this specific program is below and if it seems a bit roundabout, that is because the constraints are requiring some different methods to solve this problem. Anyways here is the code below. The problem I am having is with the add method. I am almost positive I am making it more complicated than it needs to be. The code currently in the method is not refactored. I have changed it a few times getting close to the results I need but never managing to get the results. Furthermore the return value is always going to be 8 bits for example, 11111111 + 00000001 = 11111111 and negatives do not apply. Code: ```package extraCredit; // Encapsulation of binary numbers import java.util.Scanner; class BinaryNumber { // Store the binary number as an array of boolean static final int NBITS = 8; private boolean [] bits = new boolean [NBITS]; /** Constructor initializes with low bits at end of array */ public BinaryNumber(String s) { //if the length is to short we have to append that needed 0's to the front in order to avoid string length out of range errors if (s.length() < 8){ while (s.length() != 8){ s = "0" + s; } } for (int i = bits.length - 1, j = 1 ; i >= 0; i--, j++){ if( s.charAt(s.length() - j) == '1'){ this.bits[i] = true; }else this.bits[i] = false; } } /** Convert to decimal integer value */ public int intValue() { int intValue = 0; for (int i = 0, j = 1; i < bits.length; i++, j *= 2){ if (this.bits[(bits.length - 1) - i] == true){ intValue += j; }else continue; } return intValue; } /** Determine if two BinaryNumbers are equivalent */ public boolean equals(BinaryNumber b2) { int occurences = 0; for (int i = 0; i < bits.length; i++){ if (this.bits[i] != b2.bits[i] ){ occurences++; }else continue; } if (occurences == 0){ return true; }else return false; } /** Produce String representation of BinaryNumber */ public String toString() { String returnString = new String(""); for (int i = 0; i < bits.length; i++){ if (bits[i] == true){ returnString += 1; }else returnString += 0; } return returnString; } public BinaryNumber add(BinaryNumber b2){ boolean twoCompliment[] = new boolean [8]; String sumString = new String("null"); int length = bits.length - 1; for (int i = 0, j = 1; i < bits.length; i++, j++){ if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == false){ sumString += "0"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == false){ sumString += "1"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "1"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == true){ sumString += "0"; twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "1"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == false){ sumString += "1"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == false){ sumString += "0"; twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "0"; twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == true){ sumString += "1"; twoCompliment[twoCompliment.length - j] = true; } } BinaryNumber sum = new BinaryNumber(sumString); return sum; } // Main method to test BinaryNumber public static void main(String[] args) { // Prompt for numbers if not on command line String number1, number2; if (args.length < 1) { Scanner input = new Scanner(System.in); System.out.print("Enter two eight bit binary numbers: "); number1 = input.next(); number2 = input.next(); } else { number1 = args[0]; number2 = args[1]; } BinaryNumber b1 = new BinaryNumber(number1); BinaryNumber b2 = new BinaryNumber(number2); BinaryNumber b3; System.out.println("b1 is " + b1 + " (" + b1.intValue() + ")"); System.out.println("b2 is " + b2 + " (" + b2.intValue() + ")"); System.out.println("b1.equals(b2) is " + b1.equals(b2)); b3 = b1.add(b2); System.out.println("b3=b1.add(b2) is " + b3 + " (" + b3.intValue() + ")"); b3 = b3.add(b1); System.out.println("b3=b3.add(b1) is " + b3 + " (" + b3.intValue() + ")"); b2 = b2.add(b1).add(b1); System.out.println("b2=b2.add(b1).add(b1) is " + b3 + " (" + b2.intValue() + ")"); System.out.println("b2.equals(b3) is " + b2.equals(b3)); } }```
 03-10-2011, 07:55 AM PM User | #2 spchinta New Coder   Join Date: Mar 2011 Location: USA Posts: 23 Thanks: 0 Thanked 1 Time in 1 Post How come addition of 11111111 and 00000001 is always 11111111. Is that kind of OR operation, If so, Isn't the logic simple like this. Code: ```public BinaryNumber add(BinaryNumber b2){ boolean twoCompliment[] = new boolean [8]; String sumString = new String("null"); int length = bits.length - 1; for (int i = 0, j = 1; i < bits.length; i++, j++){ if (this.bits[length - i] == false && b2.bits[length - i] == false){ sumString += "0"; twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == true){ sumString += "1"; twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == false){ sumString += "1"; twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == true ){ sumString += "1"; twoCompliment[twoCompliment.length - j] = true; } } BinaryNumber sum = new BinaryNumber(sumString); return sum; }``` I may be silly in understanding the results. If so, can you give me example of few results or waht actually you are trying to do. I wil try to find out some solution..
 03-10-2011, 08:02 AM PM User | #3 Jaxyral New Coder   Join Date: Apr 2010 Posts: 16 Thanks: 2 Thanked 0 Times in 0 Posts Of course. Here are some sample results: > java BinaryNumber 10011 101111 b1 is 00010011 (19) b2 is 00101111 (47) b1.equals(b2) is false b3=b1.add(b2) is 01000010 (66) b3=b3.add(b1) is 01010101 (85) b2=b2.add(b1).add(b1) is 01010101 (85) b2.equals(b3) is true > java BinaryNumber 00000001 11111111 b1 is 00000001 (1) b2 is 11111111 (255) b1.equals(b2) is false b3=b1.add(b2) is 00000000 (0) b3=b3.add(b1) is 00000001 (1) b2=b2.add(b1).add(b1) is 00000001 (1) b2.equals(b3) is true
 03-10-2011, 02:17 PM PM User | #4 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,598 Times in 2,567 Posts This is too complicated for what you need. Java has some really neat features that do most of this work for you in the wrapper classes. Check out the Integer.toString and Integer.parseInt. Both of these methods have a second parameter for the radix base to use. Since you're not working with actual binary here, you needn't even delve into the bit operators either; you can simply add two integers and parse them for the result string (If you wanted to add two 'integer' equivilents in binary, you'd use `(binaryNum1 | binaryNum2) + (binaryNum1 & binaryNum2)`). The only thing that *may* be complicated is writing a loop for the left padding for the 0's. That's still only a few lines of code though. __________________ PHP Code: ``` header('HTTP/1.1 420 Enhance Your Calm');  ```
 03-10-2011, 07:39 PM PM User | #5 spchinta New Coder   Join Date: Mar 2011 Location: USA Posts: 23 Thanks: 0 Thanked 1 Time in 1 Post I am not sure about binary addition api, so worked on the code part you gave to make it work. Here is the code. Code: ```public BinaryNumber add(BinaryNumber b2){ boolean twoCompliment[] = new boolean [8]; String sumString = ""; int length = bits.length - 1; for (int i = 0; i < bits.length; i++){ int j = i+2; if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == false){ sumString += "0"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == false){ sumString += "1"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "1"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == true){ sumString += "0"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "1"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == false){ sumString += "1"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = false; } else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == false){ sumString += "0"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == true){ sumString += "0"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = true; } else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == true){ sumString += "1"; if((twoCompliment.length - j)>=0) twoCompliment[twoCompliment.length - j] = true; } } BinaryNumber sum = new BinaryNumber(new StringBuffer(sumString).reverse().toString()); return sum; }``` This is working but I always suggest to use API(if we have one).

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:10 AM.

 AdvertisementLog in to turn off these ads.