CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

RE: Java - Bin2Text - Second Draft


[Replies] [Reply] [View by Thread] [Help]
[Back To SNEAK Development Forum]

Posted by Tr flamebalrog On 2005-02-26 02:06:30
In Reply to Java - Bin2Text - Second Draft Posted by Cpt SAJChurchey On 2005-02-24 20:22:31

Tr
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:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.06617 seconds


About Us | Privacy Policy | Mission Statement | Help