diff --git a/algorithms/C/README.md b/algorithms/C/README.md index debcba7f..862ac8b2 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -4,6 +4,11 @@ - [Even and Odd](arrays/even-and-odd.c) - [Unique Elements in an array](arrays/unique-elements-in-an-array.c) +## Bit Manipulation +- [Add and Subtract](bit-manipulation/add-and-sub-bitwise) +- [Multiply](bit-manipulation/multiply-bitwise) +- [divide](bit-manipulation/divide-bitwise) + ## Graphs - [Prim's Algorithm](graphs/Prim's-algorithm.c) diff --git a/algorithms/C/bit-manipulation/add-and-sub-bitwise.c b/algorithms/C/bit-manipulation/add-and-sub-bitwise.c new file mode 100644 index 00000000..44224443 --- /dev/null +++ b/algorithms/C/bit-manipulation/add-and-sub-bitwise.c @@ -0,0 +1,50 @@ +// function to add, sub without using the operators + +#include + +// Prototypes +int add(int, int); +int sub(int, int); + + +int main() { + + int a, b; + + printf("Enter two integers\n"); + + scanf("%d %d", &a, &b); + + int s,d; + + d = add(a,b); + + s = sub(a,b); + + printf("%d %d",s,d); // output 2 and 10 + + return 0; +} + +// function to add +int add(int a, int b) +{ + int c = 0; //carry + while(b !=0) + { + c = a&b; + a = a^b; + b = c<<1; + } + + return a; +} + +// function to sub +int sub(int a, int b) +{ + int s,d; + d = add((~b),1); + s = add(a,d); + return s; +} // subtraction is addition in disguise (just flip the subtracting term and that's what this function does.) diff --git a/algorithms/C/bit-manipulation/divide-bitwise.c b/algorithms/C/bit-manipulation/divide-bitwise.c new file mode 100644 index 00000000..50052d85 --- /dev/null +++ b/algorithms/C/bit-manipulation/divide-bitwise.c @@ -0,0 +1,37 @@ +// Program to divide a number without using the operator '/' + +#include +#include + +int division(int A, int B); + +int main() { + // your code goes here + int a, b; + printf("Enter the two integers"); + scanf("%d %d", &a, &b); + printf("The division is %d",division(a,b)); // Returns the floored output. +} + +int division(int divisor, int dividend) +{ + int a = abs(divisor); + int b = abs(dividend); + int result = 0; + while(a-b>=0) + { + int x=0; // b<<0 + + while(a -(b<=0) + { + x++; + } + + result += 1<0) == (dividend>0) ? result : -result ; + + +} \ No newline at end of file diff --git a/algorithms/C/bit-manipulation/multiply-bitwise.c b/algorithms/C/bit-manipulation/multiply-bitwise.c new file mode 100644 index 00000000..ecce565d --- /dev/null +++ b/algorithms/C/bit-manipulation/multiply-bitwise.c @@ -0,0 +1,30 @@ +// Shift and add algo for multiplication without using multiplication operator + +#include +#include + +int main() { + + + // your code goes here + int mA, mB; + printf("Enter the two inputs\n"); + scanf("%d %d", &mA, &mB); + int a = 0, m0 = 0, m1 = 0; + m0 = mA > 0 ? mA : abs(mA); // negative input. + m1 = mB > 0 ? mB : abs(mB); + + while(m1) + { + if((m1 & 0x01)) + a += m0; + + m0 <<= 1; + m1 >>= 1; + } + if(mA < 0 || mB < 0) + a = -a; + + printf("The product is %d", a); + return 0; +}