Skip to content

Commit cbb3118

Browse files
Merge pull request #242 from aelshinawy/c-bit-manipulation
[Snippets] Added a few bit-manipulation snippets in C
2 parents 27d8016 + 466c2d5 commit cbb3118

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Clear ith bit
3+
description: Clear the ith bit of a number and returns the resulting number
4+
tags: bit-manipulation, number, clear
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
int clear_ith_bit(int n, int i) {
10+
return n & ~(1 << i);
11+
}
12+
13+
14+
// Usage:
15+
clear_ith_bit(10, 1); // Returns: 8
16+
clear_ith_bit(10, 3); // Returns: 2
17+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Count Set Bits
3+
description: Counts the number of set bits in an int
4+
tags: bit-manipulation, count
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
int count_set_bits(int n) {
10+
int count = 0;
11+
while (n) {
12+
n &= (n - 1);
13+
count++;
14+
}
15+
return count;
16+
}
17+
18+
19+
// Usage:
20+
count_set_bits(5); // Returns: 2
21+
count_set_bits(255); // Returns: 8
22+
count_set_bits(8); // Returns: 1
23+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Get ith bit
3+
description: Get the i-th bit of a number
4+
tags: bit-manipulation, number, get
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
int get_ith_bit(int n, int i) {
10+
return (n >> i) & 1;
11+
}
12+
13+
14+
// Usage:
15+
get_ith_bit(10, 0); // Returns: 0
16+
get_ith_bit(10, 1); // Returns: 1
17+
get_ith_bit(10, 2); // Returns: 0
18+
get_ith_bit(10, 3); // Returns: 1
19+
```

snippets/c/bit-manipulation/is-odd.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Is Odd
3+
description: Check if a number is odd
4+
tags: bit-manipulation, number, is-odd
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
bool is_odd(int n) {
10+
return n & 1;
11+
}
12+
13+
14+
// Usage:
15+
is_odd(10); // Returns: false
16+
is_odd(11); // Returns: true
17+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Set ith bit
3+
description: Set the i-th bit of a number and returns the resulting number
4+
tags: bit-manipulation, number, set
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
int set_ith_bit(int n, int i) {
10+
return n | (1 << i);
11+
}
12+
13+
14+
// Usage:
15+
set_ith_bit(10, 0); // Returns: 11
16+
set_ith_bit(10, 2); // Returns: 14
17+
set_ith_bit(1, 8); // Returns: 257
18+
set_ith_bit(1, 3); // Returns: 9
19+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Swap Numbers
3+
description: Swap two numbers without a temporary variable
4+
tags: bit-manipulation, number, swap
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
void swap(int *a, int *b) {
10+
*a ^= *b;
11+
*b ^= *a;
12+
*a ^= *b;
13+
}
14+
15+
16+
// Usage:
17+
int x = 5, y = 10;
18+
swap(&x, &y);
19+
printf("x = %d, y = %d\n", x, y); // x = 10, y = 5
20+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Toggle ith bit
3+
description: Toggle the i-th bit of a number and returns the resulting number
4+
tags: bit-manipulation, number, toggle
5+
author: aelshinawy
6+
---
7+
8+
```c
9+
int toggle_ith_bit(int n, int i) {
10+
return n ^ (1 << i);
11+
}
12+
13+
14+
// Usage:
15+
toggle_ith_bit(10, 0); // Returns: 11
16+
toggle_ith_bit(10, 1); // Returns: 8
17+
toggle_ith_bit(8, 1); // Returns: 10
18+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy