RE: Java - Bin2Text - Second Draft |
||
![]() Tr flamebalrog On 2005-02-24 20:22:31, SAJChurchey wrote > > > private static int bin2decimal(String binary){ > int decimal = 0; > for(int i = binary.length() - 1;i >= 0;--i){ > int asciiValue = binary.charAt(i) - '0'; > decimal += Math.pow(2,i) * asciiValue; > }//end for > return decimal; > } > Is Math.pow() faster than shift+or ? Also, the result is an integer, not a decimal. i.e. : naming the function bin2decimal could lead to misunderstanding (like decimal<==>represented with digits [0-9], which is probably not true 'coz I doubt an int is represented like that in memory...but I've never been in there so...). private static int bin2int(String binary){
int result = 0;
for(int i=0 ; i<binary.length ; i++) {
int bitValue = binary.charAt(i) - '0';
result=(result<<1)|bitValue;
}// avoid useless comments
return result;
}
- Is it a feature or a bug ? -
Replies:
|
||
| CyberArmy::Forum v0.6 Generated In 0.06617 seconds |