Main:
public class Main{
public static void main(String[] args){
System.out.println(Convert.BtoI("101101010101"));
System.out.println(Convert.BtoI("1011110"));
}
}
Sub:
public class Convert{
public static int BtoI(String value){
int no = 0;
for(int i=value.length()-1;i>=0;i--){
if(value.charAt(i)=='1')
no += (???) ;
++;
}
return no;
}
}
How can I convert a string binary to integer without using maths.pow, just using + - * and /, should I implement another for loop for it is int j = 1;i <= example; i*=2){ ?. I am quite confused and want to learn without the usage of maths.pow or any similar codes.
x << yis effectively equivalent toMath.pow(2, y).Integer.parse(). The secret is to at each step multiply by the radix. For base 10 you multiply by 10, and for base 2 you multiply by two. Note in the duplicate link at least two answer shift by 1 at each iteration, which is the same as multiplying by 2.powmethod.