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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -