Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-10-2011, 02:08 AM   PM User | #1
Jaxyral
New Coder

 
Join Date: Apr 2010
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jaxyral is an unknown quantity at this point
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));
    }
}
Jaxyral is offline   Reply With Quote
Old 03-10-2011, 06:55 AM   PM User | #2
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Old 03-10-2011, 07:02 AM   PM User | #3
Jaxyral
New Coder

 
Join Date: Apr 2010
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jaxyral is an unknown quantity at this point
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
Jaxyral is offline   Reply With Quote
Old 03-10-2011, 01:17 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-10-2011, 06:39 PM   PM User | #5
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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).
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.