java - Converting String binary to integer -
how can without using multiplication, division or mod?
i come solution needs multiplication.
public stringbtoint(string b) { int value = 0; for(int z = 0; z < b.length(); z++) { value = value * 2 + (int)b.charat(i) - 48; } }
edit: sorry! 3 java api allowed. length(), charat(), , equals()
without multiplication, use bitwise shift operator:
public stringbtoint(string b) { int value = 0; for(int z = 0; z < b.length(); z++) { if(b.charat(z) == '1'){ shift = b.length()-z-1; value += (1 << shift); } } }
Comments
Post a Comment