FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
rachel carson wildlife preserve Transforming an int to a 2's complement binary string (1 viewing) (1) Guests
Go to bottom Post Reply Favoured: 0
TOPIC: rachel carson wildlife preserve Transforming an int to a 2's complement binary string
#6324
GeoWCherry (Visitor)
Click here to see the profile of this user
Birthdate:
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
Each of the following two methods transforms an int to a 2's complement binary string. Is there any reason to prefer one to the other? George W. Cherry http://sdml.com    public static String intToBinary1( int value ) {       StringBuffer result = new StringBuffer(32);       for( int i = 0; i < 32; i++ ) {          if(value < 0)             result.append('1');          else {             result.append('0');          }          value = value << 1;       }//end for       return result.toString();    }//end intToBinary1()    public static String intToBinary2( int value ) {       StringBuffer result = new StringBuffer(                                                 );       for( int i = 0; i < 32; i++ ) {          if( (value & 1) == 1 )             result.setCharAt(31 - i, '1');          else {             result.setCharAt(31 - i, '0');          }          value = value 1;       }//end for       return result.toString();    }//end intToBinary2()
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#6325
Paul Lutus (Visitor)
Click here to see the profile of this user
Birthdate:
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
string. Is there any reason to prefer one to the other? George W. Cherry http://sdml.com    public static String intToBinary1( int value ) {       StringBuffer result = new StringBuffer(32);       for( int i = 0; i < 32; i++ ) {          if(value < 0)             result.append('1');          else {             result.append('0');          }          value = value << 1;       }//end for       return result.toString();    }//end intToBinary1()    public static String intToBinary2( int value ) {       StringBuffer result = new StringBuffer(                                                 );       for( int i = 0; i < 32; i++ ) {          if( (value & 1) == 1 )             result.setCharAt(31 - i, '1');          else {             result.setCharAt(31 - i, '0');          }          value = value 1;       }//end for       return result.toString();    }//end intToBinary2() Here's an idea. You tell us which you think is better, and why, then we will discuss your reply. We don't do homework here. You could learn a whole lot by compiling and testing the two methods. Have you considered this ingenious approach? BTW there is a one-line solution in Java for this conversion.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#6326
Thomas Ĺhlen (Visitor)
Click here to see the profile of this user
Birthdate:
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
string. Is there any reason to prefer one to the other? George W. Cherry http://sdml.com    public static String intToBinary1( int value ) {       StringBuffer result = new StringBuffer(32);       for( int i = 0; i < 32; i++ ) {          if(value < 0)             result.append('1');          else {             result.append('0');          }          value = value << 1;       }//end for       return result.toString();    }//end intToBinary1()    public static String intToBinary2( int value ) {       StringBuffer result = new StringBuffer(                                                 );       for( int i = 0; i < 32; i++ ) {          if( (value & 1) == 1 )             result.setCharAt(31 - i, '1');          else {             result.setCharAt(31 - i, '0');          }          value = value 1;       }//end for       return result.toString();    }//end intToBinary2()
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#6327
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
string. Is there any reason to prefer one to the other? George W. Cherry Paul Lutus <<Here's an idea. You tell us which you think is better, and why, then we will discuss your reply. We don't do homework here. Good idea, Paul. I prefer intToBinary1() to intToBinary2() because it is shorter, simpler, and runs a little faster. Alas, my question is not homework: I graduated from MIT many decades ago and retired from MIT awhile ago also. (I was Associate Director of the Draper laboratory. One of my jobs there was project manager of the Apollo Primary Navigation, Guidance, and Control System: MIT was the prime contractor to NASA for the PNGCS.) Now I'm just noodling around here and working on my fifth book. (Oh yes, I also push over dead trees in the Rachel Carson Wildlife Refuge in Maine, near where I mostly live.) Paul Lutus <<You could learn a whole lot by compiling and testing the two methods. Have you considered this ingenious approach? Another good idea, Paul. I had already done that, but I just added a benchmark of Mr. Babu Kalakrishnan's code, which ran faster than my similar algorithm, probably because he used a char[] and I used a StringBuffer. BTW, I like his code (intToBinary0) most of the three: it's shortest and fastest. intToBinary0() took 2690 milliseconds. Babu Kalakrishnan's code intToBinary1() took 4180 milliseconds. intToBinary2() took 4890 milliseconds. Paul Lutus <<BTW there is a one-line solution in Java for this conversion. If you don't mean Integer.toBinaryString(), I hope you'll post it. The three methods are below. George W. Cherry http://sdml.com  (book manu_script_ waiting for reviewers)    public static String intToBinary0( int value ) {       char[] result = new char[32];       for (int i = 0; i < 32; i = i + 1) {          result[i] = (value < 0) ? '1' : '0' ;          value = value << 1;       }       return new String(result);    }    public static String intToBinary1( int value ) {       StringBuffer result = new StringBuffer(32);       for( int i = 0; i < 32; i++ ) {          if(value < 0)             result.append('1');          else {             result.append('0');          }          value = value << 1;       }//end for       return result.toString();    }//end intToBinary1()    public static String intToBinary2( int value ) {       StringBuffer result = new StringBuffer(                                                 );       for( int i = 0; i < 32; i++ ) {          if( (value & 1) == 1 )             result.setCharAt(31 - i, '1');          else {             result.setCharAt(31 - i, '0');          }          value = value 1;       }//end for       return result.toString();    }//end intToBinary2()
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#6328
Lee Fesperman (Visitor)
Click here to see the profile of this user
Birthdate:
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
Paul Lutus <<BTW there is a one-line solution in Java for this conversion. If you don't mean Integer.toBinaryString(), I hope you'll post it. How about with Long.toBinaryString()? ... Long.toBinaryString(((long) value & 0xffffffffl) | 0x100000000l).substring(1)
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#6329
rachel carson wildlife preserve Transforming an int to a 2's complement binary string  
intToBinary0() took 2690 milliseconds. Babu Kalakrishnan's code intToBinary1() took 4180 milliseconds. intToBinary2() took 4890 milliseconds.   public static String intToBinary0( int value ) {      char[] result = new char[32];      for (int i = 0; i < 32; i = i + 1) {         result[i] = (value < 0) ? '1' : '0' ;         value = value << 1;      }      return new String(result);   } If you're looking at optimization, you might just be able to squeeze out a little bit more on with the following - at the cost of an extra couple of lines of code. public static String intToBinary3 (int value) {     char[] result = new char[32];     int i=0;     while (value != 0)     {         result[i++] = (value < 0) ? '1' : '0';         value = value << 1;     }     while (i < 32) result[i++]='0'; } I haven't benchmarked it - but this should save at least 1 shift on all even numbers, 2 shifts and 1 comparison for all multiples of 4, and so on (till for the single number 0 where it saves 32 shifts and 31 comparisons) at the cost of one extra integer comparison for all odd numbers. So on the average I guess it should be slightly faster over a uniformly distributed integer sample. BK
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop
 

Who's Online

We have 40 guests online
Google Earth 5.0 Google announced today the introduction of Google Ocean to its Google Earth geographical applications 5 Virtual diving, exploring three-dimensional underwater terrain, and view the material on the deep sea, prepared by ocean scietists - these are just some of the possibilities offered by Google Ocean. Oceans cover over 70% of the Earth's surface, and the hidden depths of 80% of life on our planet. The man managed to Krakow travel mercedescarsite.co.uk teppiche not only meet 5% of that space. By combining unique images from the comments of experts, ocean in Google Earth allows users to easily reach the most difficult places available on the planet. Virtual explorers can swim around underwater volcanoes, watch film footage of underwater life exotic, read the information about the nearby wrecks of ships, as well as upload pictures and videos of their favorite places to dive or surf.

YouTube's serious problems approaching ? It seems that soon will disappear quite a large number of very Great car reviews Katowice airport transfers erotic popular music videos by artists such as Madonna, Red Hot Chili Peppers, and Led Zeppelin. This will be the result of the absence of a compromise between Google and the record company Warner Music Group, which demanded a greater share of the profits. Recently, one of the largest record companies - Warner Music Group has expressed its displeasure by too small a share Polish Pottery nice pics properties for sale in spain in profits generated by Youtube using such materials that label. The problem, however, is that Google is trying to how is that YouTube finally started to earn his.

Wooden Nokia Nokia's Eco-Team, a group of workers developing a more environmentally friendly phone concept, designed the wooden kamerofon with 8 megapixel camera. Embedded in a wooden casing for Nokia looks quite rustic, and although difficult to actually call it organic, could be an interesting alternative to traditional telephones. Phone is made of wood from forests with sustainable management, Alveo Drink get-carfinance.co.uk Volgograd photo which is obtained mainly from the spirit of eco (although it would be more environmentally friendly device made of bamboo for example). Since this is only a concept we do not have too much information about the functionalities, in addition to 8 megapixel camera, Bluetooth, and that the phone has to work on Symbian S60 used cars vehicles.online-auto.co.uk faucet . The phone sees the light of day, of course, is not known yet.