Advertisement
T99

Deprecated Decimal to Binary Converter

T99
Feb 3rd, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. // Deprecated Decimal to Binary Converter
  2.  
  3. ArrayList<Boolean> binary = new ArrayList<>();
  4. binary.add(true);
  5. int factor = 0;
  6. int i1 = 1;
  7. boolean found = false;
  8.  
  9. if (dec == 0) {
  10.    
  11.     return new Binary(new Boolean[] {false, false, false, false, false, false, false, false});
  12.    
  13. }
  14.        
  15. while (!found) {
  16.    
  17.     if (i1 * 2 > dec) {
  18.        
  19.         factor = i1;
  20.         found = !found;
  21.        
  22.     } else {
  23.                
  24.         binary.add(false);
  25.         i1 *= 2;
  26.                
  27.     }
  28.            
  29. }
  30.        
  31. dec -= factor;
  32.        
  33. for (int i2 = new Double(Math.log(factor)/Math.log(2)).intValue(); i2 >= 0; i2--) {
  34.            
  35.     if (factor <= dec) {
  36.                
  37.         dec -= factor;
  38.         binary.set(binary.size() - (i2 + 1), true);
  39.                
  40.     }
  41.            
  42.     factor /= 2;
  43.            
  44. }
  45.        
  46. while (binary.size() % 8 != 0) {
  47.            
  48.     binary.add(0, false);
  49.            
  50. }
  51.    
  52. return new Binary(binary);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement