Showing posts with label Number System. Show all posts
Showing posts with label Number System. Show all posts

Number System - Covert a Decimal Number to Octal and Hex Number

Oct 23, 2015

It is  very interesting topic. If we simple think, the process of conversion from decimal number to Octal number, then we can follow the below steps:
1. Let x = 15
2. r[0] = x%8 = 7
3. x = x/8 = 1
4. r[1] = x%8 = 1;
5. x = x/8 = 0
Terminate the algorithm, we find (15)10 = (17)8 [Note: reverse r]
But if we use bit-wise operator, then it makes more interesting and efficient.

Number Conversion -1 : Decimal to Binary

Oct 21, 2015

At first glance, we think about the below algorithm
1. take input x
2. r[i] = x%2;
3. x = x/2
4. do above steps until x =0

the complexity of this algorithm is O(lgn) , where n = decimal number.

Convert a decimal Number to Binary Without array in C

Sep 29, 2015

Algorithm:
1. take input x
2. Initialize r := 0,cb = 0;
3. while(x!=0) Begin
4. cb := cb+1, r := left shift by 1 bit
5. r :=r + (x&1)
6. n:= right shift by 1;
7. End