Jason arora
Jason arora

Reputation: 550

Output for Converting a number in decimal into its Complement

Output for converting a number in decimal into its 1s complement and then again converting the number into decimal does not come as expected.

MyApproach

I first converted the number from decimal to binary. Replaced all Os with 1 and vice versa and then converted the number into decimal.

Can anyone guide me? What I am doing wrong?

Code:

public static int complimentDecimal(int num) {
    int p = 0;
    String s1 = "";
    // Convert Decimal to Binary

    while (num > 0) {
        p = num % 2;
        s1 = p + s1;
        num = num / 2;
    }
    System.out.println(s1);
    // Replace the 0s with 1s and 1s with 0s
    for (int j = 0; j < s1.length(); j++) {
        if (s1.charAt(j) == 0) {
            s1.replace(s1.charAt(j), '1');
        } else {
            s1.replace(s1.charAt(j), '0');
        }

    }
    System.out.println(s1);

    int decimal = 0;
    int k = 0;
    for (int m = s1.length() - 1; m >= 0; m--) {
        decimal += (s1.charAt(m) * Math.pow(2, k));

        k++;
    }
    return decimal;
}

Upvotes: 0

Views: 1022

Answers (4)

ArcticLord
ArcticLord

Reputation: 4039

First of all you need to define the amount of Bits your binary representation should have or an complement representation does not make sense.

If you convert 100 the binary is 1100100
complement is 0011011 which is 27
now convert 27. Binary is 11011, complement 00100 which is 4.

Now define yourself a Bit length of 8.
100 is 01100100, complement 10011011, is 155
155 is 10011011, complement 01100100, is 100
Works because every binary representation has a length of 8 bits. This is absolutly necessary for the whole complement thing to make any sense.
Consider that you now have a limit for numbers that are convertable.
11111111 which is 255.

Now that we talked about that I will correct your code

static int MAX_BITS = 8;
static int MAX_INT = (int)Math.pow(2, MAX_BITS) - 1;

public static int complimentDecimal(int num)
{
    // check if number is to high for the bitmask
    if(num > MAX_INT){
        System.out.println("Number=" + num + " to high for MAX_BITS="+MAX_BITS);
        return -1;
    }

    // Your conversion works!
    int p=0;
    String s1="";
    //Convert Decimal to Binary
    while(num>0)
    {
        p=num%2;
        s1=p+s1;
        num=num/2;
    }

    // fill starting zeros to match MAX_BITS length
    while(s1.length() < MAX_BITS)
        s1 = "0" + s1;

    System.out.println(s1);

    //Replace the 0s with 1s and 1s with 0s

    // your approach on that is very wrong
    StringBuilder sb = new StringBuilder();
    for(int j=0;j<s1.length();j++){
        if(s1.charAt(j)=='0') sb.append("1");
        else if(s1.charAt(j)=='1') sb.append("0");
    }
    s1 = sb.toString();

    /*
    for(int j=0;j<s1.length();j++)
    {
        if(s1.charAt(j)==0)
        {
            s1.replace(s1.charAt(j),'1');
        }
        else
        {
            s1.replace(s1.charAt(j),'0');
        }

    }
    */
    System.out.println(s1);

    int decimal=0;
    int k=0;
    for(int m=s1.length()-1;m>=0;m--)
    {
        // you don't want the char code here but the int value of the char code
        //decimal += (s1.charAt(m) * Math.pow(2, k));
        decimal+=(Character.getNumericValue(s1.charAt(m))*Math.pow(2, k));

        k++;
    } 
    return decimal;
}

Additional Note: Don't get bigger then MAX_BITS = 31 or you need to work with long instead of int in your method.

Upvotes: 2

Lahiru Jayathilake
Lahiru Jayathilake

Reputation: 601

First of all you have to assign the replaced String to the already defined variable that is,

s1.replace(s1.charAt(j),'1');

it should be

s1 = s1.replace(s1.charAt(j),'1');

and the next case is, when you are changing in that order it would change all the characters similar to matched case refer Replace a character at a specific index in a string?

Upvotes: 2

ajb
ajb

Reputation: 31689

The problem (OK, one of the problems) is here:

if(s1.charAt(j)==0)

Characters in Java are actually integers, in the range 0 to 65535. Each of those numbers actually means the character corresponding to that number in the Unicode chart. The character '0' has the value 48, not 0. So when you've created a string of '0' and '1' characters, the characters will have the integer values 48 and 49. Naturally, when you compare this to the integer 0, you'll get false no matter what.

Try

if(s1.charAt(j)=='0')

(Note: OK, the other answer is right--replace does not work. Not only are you using it incorrectly, by not assigning the result, it's not the right method anyway, because s1.replace(s1.charAt(j),'1') replaces all '0' with '1' characters; it doesn't replace character j. If you specifically want to replace the j'th character in a String with something else, you'll need to use substring() and build a new string, not replace().)

A couple other things to note: (1) Integers are not "decimal" or "binary". When your method gets the num parameter, this is just a number, not a decimal number or a binary number. It's represented in your computer as a binary number (unless you're using something like a Burroughs 3500, but I think all of those died before Java was invented). But it really isn't considered decimal, binary, octal, hex, ternary, or whatever, until you do something that converts it to a String. (2) I know you said not to post alternative approaches, but you could replace the entire method with just one line: return ~num;. That complements all the bits. If you were thinking that you couldn't do this because num was a decimal number, see #1. (3) "Compliment" means to say something nice about somebody. If you're talking about flipping all the bits, the correct spelling is "complement".

Upvotes: 1

Subhankar
Subhankar

Reputation: 692

String.Replace(oldChar, newChar) method returns a new string resulting from replacing all occurrences of oldChar in given string with newChar. It does not perform change on the given string.

Upvotes: 1

Related Questions