From 7635acc38a530522bd0fff70b3ad23da1ced19c5 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Mon, 10 Oct 2022 15:32:53 -0700 Subject: [PATCH 1/5] Smaller and faster way to calculate modulus of 2 using bit manipulation --- .../modulus_with_power_of_2.c | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 algorithms/bit-manipulation/modulus_with_power_of_2.c diff --git a/algorithms/bit-manipulation/modulus_with_power_of_2.c b/algorithms/bit-manipulation/modulus_with_power_of_2.c new file mode 100644 index 00000000..a1346e4a --- /dev/null +++ b/algorithms/bit-manipulation/modulus_with_power_of_2.c @@ -0,0 +1,20 @@ +/* + 1. Here is an example of the modulus operation + using bit manipulation + 2. It is supposed to be much faster and more efficient + The only condition is that n should be a power of 2 +*/ + +#include + +#define modulo(x,n) ((x) & (n - 1)) + +int main() +{ + printf("Hello World\n"); + + for(int i = 0; i < 100; i++) + printf("value of %d modulo 8 is: %d\n", i, modulo(i,10)); + + return 0; +} From 270e3d71ec6e526aa3c1968b2c49ffe06d0262e4 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Mon, 10 Oct 2022 15:48:48 -0700 Subject: [PATCH 2/5] Checking for power of 2 using bit manipulation --- algorithms/bit-manipulation/check_power_of_2.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 algorithms/bit-manipulation/check_power_of_2.c diff --git a/algorithms/bit-manipulation/check_power_of_2.c b/algorithms/bit-manipulation/check_power_of_2.c new file mode 100644 index 00000000..fd7baef3 --- /dev/null +++ b/algorithms/bit-manipulation/check_power_of_2.c @@ -0,0 +1,15 @@ +#include + +#define check_power_of_2(x) (!( x & (x - 1)) && (x > 1)) + +int main() +{ + for(int i = 1; i < 100; i++) + { + if(check_power_of_2(i)) + printf("True! %d is a power of 2\n", i); + else + printf("False! %d is not a power of 2\n", i); + } + printf("Hello World\n"); +} \ No newline at end of file From 3e54f23f6f51bc34241271ea366309d682b4aad1 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Mon, 10 Oct 2022 16:00:28 -0700 Subject: [PATCH 3/5] addition using bit manipulation without using the '+' operator --- algorithms/bit-manipulation/addition.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 algorithms/bit-manipulation/addition.c diff --git a/algorithms/bit-manipulation/addition.c b/algorithms/bit-manipulation/addition.c new file mode 100644 index 00000000..5d6f65eb --- /dev/null +++ b/algorithms/bit-manipulation/addition.c @@ -0,0 +1,20 @@ +#include + +int add(int x, int y) +{ + while(x > 0) + { + unsigned int carry = x & y; + y = x ^ y; + x = carry << 1; + } + return y; +} + +int main() +{ + printf("sum of %d and %d is %d\n", 3, 4, add(3, 4)); + return 0; +} + + From f82cf4342e54f14d0a8be24353416487f63b7157 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Mon, 10 Oct 2022 16:01:54 -0700 Subject: [PATCH 4/5] subtraction using bit manipulation without using the '-' operator --- algorithms/bit-manipulation/subtraction.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 algorithms/bit-manipulation/subtraction.c diff --git a/algorithms/bit-manipulation/subtraction.c b/algorithms/bit-manipulation/subtraction.c new file mode 100644 index 00000000..23f7d2a8 --- /dev/null +++ b/algorithms/bit-manipulation/subtraction.c @@ -0,0 +1,20 @@ +#include + +int subtract(int x, int y) +{ + while(y != 0) + { + unsigned int borrow = y & ~x; + x = y ^ x; + y = borrow << 1; + } + return x; +} + +int main() +{ + printf("diff of %d and %d is %d\n", 4, 3, subtract(4, 3)); + return 0; + + + From 1d492b5241670365bc497c233a3afe502af1d871 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Mon, 10 Oct 2022 16:07:49 -0700 Subject: [PATCH 5/5] Minor correction --- algorithms/bit-manipulation/subtraction.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/algorithms/bit-manipulation/subtraction.c b/algorithms/bit-manipulation/subtraction.c index 23f7d2a8..989ac555 100644 --- a/algorithms/bit-manipulation/subtraction.c +++ b/algorithms/bit-manipulation/subtraction.c @@ -15,6 +15,4 @@ int main() { printf("diff of %d and %d is %d\n", 4, 3, subtract(4, 3)); return 0; - - - +} \ No newline at end of file