Showing posts with label Programming in C. Show all posts
Showing posts with label Programming in C. Show all posts

Peasant or binary multiplication

Oct 1, 2015

Peasant or binary multiplication has been widely used among those who are unschooled and thus have not memorized the multiplication tables required by long multiplication. The algorithm was also in use in ancient Egypt [Wiki]. :)

Explain:
suppose,
the task is to find the value of 12 x 7

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

Count Bits of a Number in C

It is very simple to determine the number of bits of an integer number:
Algorithm:
1. take input x, initialize countbit : = 0
2. check x is zero, yes then go to step 5
3. x: = Shift one bit left of x,countbit:=countbit+1;
4. go to step 2.
5. End