From 466c2d5a3c5da74a9f631e540827a84261f20454 Mon Sep 17 00:00:00 2001 From: Ahmed El-Shinawy Date: Wed, 22 Jan 2025 13:59:50 +0200 Subject: [PATCH] Added a few bit-manipulation snippets in C --- snippets/c/bit-manipulation/clear-ith-bit.md | 17 ++++++++++++++ snippets/c/bit-manipulation/count-set-bits.md | 23 +++++++++++++++++++ snippets/c/bit-manipulation/get-ith-bit.md | 19 +++++++++++++++ snippets/c/bit-manipulation/is-odd.md | 17 ++++++++++++++ snippets/c/bit-manipulation/set-ith-bit.md | 19 +++++++++++++++ snippets/c/bit-manipulation/swap-numbers.md | 20 ++++++++++++++++ snippets/c/bit-manipulation/toggle-ith-bit.md | 18 +++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 snippets/c/bit-manipulation/clear-ith-bit.md create mode 100644 snippets/c/bit-manipulation/count-set-bits.md create mode 100644 snippets/c/bit-manipulation/get-ith-bit.md create mode 100644 snippets/c/bit-manipulation/is-odd.md create mode 100644 snippets/c/bit-manipulation/set-ith-bit.md create mode 100644 snippets/c/bit-manipulation/swap-numbers.md create mode 100644 snippets/c/bit-manipulation/toggle-ith-bit.md diff --git a/snippets/c/bit-manipulation/clear-ith-bit.md b/snippets/c/bit-manipulation/clear-ith-bit.md new file mode 100644 index 00000000..497f1a5c --- /dev/null +++ b/snippets/c/bit-manipulation/clear-ith-bit.md @@ -0,0 +1,17 @@ +--- +title: Clear ith bit +description: Clear the ith bit of a number and returns the resulting number +tags: bit-manipulation, number, clear +author: aelshinawy +--- + +```c +int clear_ith_bit(int n, int i) { + return n & ~(1 << i); +} + + +// Usage: +clear_ith_bit(10, 1); // Returns: 8 +clear_ith_bit(10, 3); // Returns: 2 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/count-set-bits.md b/snippets/c/bit-manipulation/count-set-bits.md new file mode 100644 index 00000000..97e65975 --- /dev/null +++ b/snippets/c/bit-manipulation/count-set-bits.md @@ -0,0 +1,23 @@ +--- +title: Count Set Bits +description: Counts the number of set bits in an int +tags: bit-manipulation, count +author: aelshinawy +--- + +```c +int count_set_bits(int n) { + int count = 0; + while (n) { + n &= (n - 1); + count++; + } + return count; +} + + +// Usage: +count_set_bits(5); // Returns: 2 +count_set_bits(255); // Returns: 8 +count_set_bits(8); // Returns: 1 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/get-ith-bit.md b/snippets/c/bit-manipulation/get-ith-bit.md new file mode 100644 index 00000000..f4143d5c --- /dev/null +++ b/snippets/c/bit-manipulation/get-ith-bit.md @@ -0,0 +1,19 @@ +--- +title: Get ith bit +description: Get the i-th bit of a number +tags: bit-manipulation, number, get +author: aelshinawy +--- + +```c +int get_ith_bit(int n, int i) { + return (n >> i) & 1; +} + + +// Usage: +get_ith_bit(10, 0); // Returns: 0 +get_ith_bit(10, 1); // Returns: 1 +get_ith_bit(10, 2); // Returns: 0 +get_ith_bit(10, 3); // Returns: 1 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/is-odd.md b/snippets/c/bit-manipulation/is-odd.md new file mode 100644 index 00000000..754f7684 --- /dev/null +++ b/snippets/c/bit-manipulation/is-odd.md @@ -0,0 +1,17 @@ +--- +title: Is Odd +description: Check if a number is odd +tags: bit-manipulation, number, is-odd +author: aelshinawy +--- + +```c +bool is_odd(int n) { + return n & 1; +} + + +// Usage: +is_odd(10); // Returns: false +is_odd(11); // Returns: true +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/set-ith-bit.md b/snippets/c/bit-manipulation/set-ith-bit.md new file mode 100644 index 00000000..de1d86c3 --- /dev/null +++ b/snippets/c/bit-manipulation/set-ith-bit.md @@ -0,0 +1,19 @@ +--- +title: Set ith bit +description: Set the i-th bit of a number and returns the resulting number +tags: bit-manipulation, number, set +author: aelshinawy +--- + +```c +int set_ith_bit(int n, int i) { + return n | (1 << i); +} + + +// Usage: +set_ith_bit(10, 0); // Returns: 11 +set_ith_bit(10, 2); // Returns: 14 +set_ith_bit(1, 8); // Returns: 257 +set_ith_bit(1, 3); // Returns: 9 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/swap-numbers.md b/snippets/c/bit-manipulation/swap-numbers.md new file mode 100644 index 00000000..14a377f1 --- /dev/null +++ b/snippets/c/bit-manipulation/swap-numbers.md @@ -0,0 +1,20 @@ +--- +title: Swap Numbers +description: Swap two numbers without a temporary variable +tags: bit-manipulation, number, swap +author: aelshinawy +--- + +```c +void swap(int *a, int *b) { + *a ^= *b; + *b ^= *a; + *a ^= *b; +} + + +// Usage: +int x = 5, y = 10; +swap(&x, &y); +printf("x = %d, y = %d\n", x, y); // x = 10, y = 5 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/toggle-ith-bit.md b/snippets/c/bit-manipulation/toggle-ith-bit.md new file mode 100644 index 00000000..0c3fd0fa --- /dev/null +++ b/snippets/c/bit-manipulation/toggle-ith-bit.md @@ -0,0 +1,18 @@ +--- +title: Toggle ith bit +description: Toggle the i-th bit of a number and returns the resulting number +tags: bit-manipulation, number, toggle +author: aelshinawy +--- + +```c +int toggle_ith_bit(int n, int i) { + return n ^ (1 << i); +} + + +// Usage: +toggle_ith_bit(10, 0); // Returns: 11 +toggle_ith_bit(10, 1); // Returns: 8 +toggle_ith_bit(8, 1); // Returns: 10 +``` \ No newline at end of file