-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44d6929
commit 5590e0c
Showing
58 changed files
with
989 additions
and
979 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
text eol=lf | ||
* text=auto | ||
*.js text eol=lf | ||
*.jsx text eol=lf | ||
*.ts text eol=lf | ||
*.tsx text eol=lf | ||
*.json text eol=lf | ||
*.css text eol=lf | ||
*.md text eol=lf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +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 | ||
--- | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
--- | ||
title: Remove duplicates | ||
description: Removes duplicate elements from an list | ||
author: Mcbencrafter | ||
tags: list,duplicates,unique | ||
--- | ||
|
||
```java | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public static <T> List<T> removeDuplicates(List<T> list) { | ||
return list.stream() | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// Usage: | ||
List<Integer> list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); | ||
List<Integer> result = removeDuplicates(list); | ||
System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5] | ||
--- | ||
title: Remove duplicates | ||
description: Removes duplicate elements from an list | ||
author: Mcbencrafter | ||
tags: list,duplicates,unique | ||
--- | ||
|
||
```java | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public static <T> List<T> removeDuplicates(List<T> list) { | ||
return list.stream() | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// Usage: | ||
List<Integer> list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); | ||
List<Integer> result = removeDuplicates(list); | ||
System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5] | ||
``` |
Oops, something went wrong.