Guest User

Untitled

a guest
Jan 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. package com.knowledgeblackbelt.students.ideynega.exercises;
  2.  
  3. public class Exercise284 {
  4.     private static long getBase2DecimalRepresentation(int num) {
  5.         long result = 0;
  6.         long mult = 1;
  7.         while(num / 2 > 0) {
  8.             result += num % 2 * mult;
  9.             mult *= 10;
  10.             num /= 2;
  11.         }
  12.         if(num % 2 == 1) {
  13.             result += mult;
  14.         }
  15.         return result;
  16.     }
  17.     public static void main(String[] args) {
  18.         System.out.println(getBase2DecimalRepresentation(8));
  19.     }
  20. }
  21.  
  22. /*
  23. Write a method that receives a small integer in base 10 and prints out the original number represented in base 2.
  24.  
  25. For example, giving 8 will print out "1000".
  26. */
Add Comment
Please, Sign In to add comment