Skip to content

Latest commit

 

History

History
98 lines (68 loc) · 2.07 KB

Number System.md

File metadata and controls

98 lines (68 loc) · 2.07 KB

Number System

Number System Image

Decimal-Binary Conversion

Ways to Convert Decimal to Binary

  1. Binary Timeline
  1. Division Method

Algorithm for decimal to binary (Not Optimised)

  1. Take input from user
  2. loop until num > 0
  3. rem = num %2
  4. num = num /2
  5. res = rem + res
  6. end loop
  7. print res

Code

String res ="";

while(num>0){
    int rem = num % 2 ;
    num = num/2;
    res = rem +res;
}

System.out.println("Binary of the number is: "+res);

Optimised Conversion at Bit Level

Algorithm toBinary

  1. Take the last bit with AND Operator(&1)
  2. Prepend to the result
  3. Right Shift by 1 (>>)
  4. Do it till number become 0

Algorithm toDecimal

  1. Take the last digit (%10)
  2. if last digit is 1
  3. add 2^index in the result
  4. Do it till number become 0
public class convertor {

    static void toBinary(int decimal) {

        String result = "";

        while (decimal > 0) {

            int lastbit = decimal & 1;
            result = lastbit + result;
            decimal = decimal >> 1;
        }

        System.out.println(result);
    }

    static void toDecimal(int binary){
        int result =0;
        int index =0;

        while(binary>0){
            int lastdigit = binary%10;
            if(lastdigit == 1){
                result = result + (int) Math.pow(2,index);
            }
            index++;
            binary = binary/10;
        }

        System.out.println(result);
    }

    public static void main(String args[]) {
        toBinary(12);
        toDecimal(1100);
    }

}