简体   繁体   中英

Converting a binary string to integer using a basic mathematical operator

Main:

public class Main{                                      
  public static void main(String[] args){                                       
    System.out.println(Convert.BtoI("10001"));                                  
    System.out.println(Convert.BtoI("101010101"));                                              
  }                                     
}

Class:

public class Convert{                                       
  public static int BtoI(String num){                                       
    Integer i= Integer.parseInt(num,2);                                     
    return i;                                       
  }                                     
}

So I was working on converters, I was struggling as I am new to java and my friend suggested using integer method, which works. However, which method would be most efficient to convert using the basic operators (eg logical, arithmetic etc.)

.... my friend suggested using integer method, which works.

Correct:

  • it works, and
  • it is the best way.

However, which method would be most efficient to convert using the basic operators (eg logical, arithmetic etc.)

  • If you are new to Java, you should not be obsessing over the efficiency of your code. You don't have the intuition.

  • You probably shouldn't optimize this it even if you are experienced. In most cases, small scale efficiencies are irrelevant, and you are better off using a profiler to validate your intuition about what is important before you start to optimize.

  • Even if this is a performance hotspot in your application, the Integer.parseint code has (no doubt) already been well optimized. There is little chance that you could do significantly better using "primitive" operations. (Under the hood, the methods will most likely already be doing the same thing as you would be doing.)


If you are just asking this because you are curious, take a look at the source code for the Integer class.

If you want to use basic arithmetic to convert binary numbers to integers then you can replace the BtoI() method within the class Convert with the following code.

public static int BtoI(String num){                                       
        int number = 0; // declare the number to store the result
        int power = 0; // declare power variable

        // loop from end to start of the binary number
        for(int i = num.length()-1; i >= 0; i--)
        {
            // check if the number encountered is 1
            /// if yes then do 2^Power and add to the result
            if(num.charAt(i) == '1')
                number += Math.pow(2, power);
            // increment the power to use in next iteration
            power++;
        }
        // return the number
        return number;
      }  

Normal calculation is performed in above code to get the result. eg 101 => 1*2^2 + 0 + 1*2^0 = 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM