-
-
Notifications
You must be signed in to change notification settings - Fork 130
Added code snippets of C++ on bit manipulation, math & number, searching, sorting #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16ad0a7
Add code snippets for math-and-numbers, searching, sorting, bit manip…
ashukr07 53ce70c
Add code snippets for math-and-numbers, searching, sorting, bit manip…
ashukr07 313f441
Merge branch 'dostonnabotov:main' into main
ashukr07 f95b751
move bit manipulation to c
ashukr07 5a6d50b
moved some snippets from C++ to C
ashukr07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: Check Power of Two | ||
description: Checks if a given number is a power of two using bitwise operations. | ||
tags: bit-manipulation, power-of-two | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
bool is_power_of_two(int n) { | ||
return n > 0 && (n & (n - 1)) == 0; | ||
} | ||
|
||
// Usage: | ||
is_power_of_two(16); // Returns: true | ||
is_power_of_two(18); // Returns: false | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Count Set Bits | ||
description: Counts the number of 1 bits in the binary representation of a number. | ||
tags: bit-manipulation, set-bits | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int count_set_bits(int n) { | ||
int count = 0; | ||
while (n) { | ||
count += n & 1; | ||
n >>= 1; | ||
} | ||
return count; | ||
} | ||
|
||
// Usage: | ||
count_set_bits(13); // Returns: 3 (Binary: 1101) | ||
``` |
20 changes: 20 additions & 0 deletions
20
snippets/cpp/bit-manipulation/find-non-repeating-number.md
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Find Non-Repeating Number | ||
description: Finds the number that appears only once in an array where every other number appears twice. | ||
tags: bit-manipulation, xor | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int find_non_repeating(const std::vector<int>& nums) { | ||
int result = 0; | ||
for (int num : nums) { | ||
result ^= num; | ||
} | ||
return result; | ||
} | ||
|
||
// Usage: | ||
std::vector<int> nums = {4, 1, 2, 1, 2}; | ||
find_non_repeating(nums); // Returns: 4 | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: Reverse Bits | ||
description: Reverses the bits of a given integer. | ||
tags: bit-manipulation, reverse-bits | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
unsigned int reverse_bits(unsigned int n) { | ||
unsigned int result = 0; | ||
for (int i = 0; i < 32; ++i) { | ||
result <<= 1; | ||
result |= n & 1; | ||
n >>= 1; | ||
} | ||
return result; | ||
} | ||
|
||
// Usage: | ||
reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000) | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: XOR of Range | ||
description: Finds XOR of all numbers from 1 to n using properties of XOR. | ||
tags: bit-manipulation, xor | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int xor_upto_n(int n) { | ||
if (n % 4 == 0) return n; | ||
if (n % 4 == 1) return 1; | ||
if (n % 4 == 2) return n + 1; | ||
return 0; | ||
} | ||
|
||
// Usage: | ||
xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1) | ||
``` |
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Binary to Decimal Conversion | ||
description: Converts a binary number represented as a string to its decimal equivalent. | ||
tags: binary, conversion | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int binary_to_decimal(const std::string& binary) { | ||
int decimal = 0; | ||
int base = 1; // Base value for the least significant bit | ||
|
||
for (int i = binary.length() - 1; i >= 0; --i) { | ||
if (binary[i] == '1') { | ||
decimal += base; | ||
} | ||
base *= 2; // Move to the next power of 2 | ||
} | ||
return decimal; | ||
} | ||
|
||
// Usage: | ||
std::string binary = "1011"; // Binary representation of 11 | ||
binary_to_decimal(binary); // Returns: 11 | ||
``` |
ashukr07 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Check Perfect Number | ||
description: Checks if a number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). | ||
tags: math, perfect-number | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
bool is_perfect(int n) { | ||
int sum = 1; // 1 is a divisor for all n > 1 | ||
for (int i = 2; i * i <= n; ++i) { | ||
if (n % i == 0) { | ||
sum += i; | ||
if (i != n / i) sum += n / i; | ||
} | ||
} | ||
return sum == n && n != 1; | ||
} | ||
|
||
// Usage: | ||
is_perfect(28); // Returns: true | ||
``` |
Mathys-Gasnier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: Compound Interest | ||
description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period. | ||
tags: math, finance | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
#include <cmath> | ||
|
||
double compound_interest(double principal, double rate, double time, int n) { | ||
return principal * std::pow(1 + rate / n, n * time); | ||
} | ||
|
||
// Usage: | ||
double principal = 1000.0; // Initial amount | ||
double rate = 0.05; // Annual interest rate (5%) | ||
double time = 2; // Time in years | ||
int n = 4; // Compounded quarterly | ||
compound_interest(principal, rate, time, n); // Returns: 1104.081632653061 | ||
``` |
Mathys-Gasnier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: Factorial | ||
description: Calculates the factorial of a given non-negative integer. | ||
tags: math, factorial | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int factorial(int n) { | ||
if (n <= 1) return 1; // Base case | ||
return n * factorial(n - 1); // Recursive step | ||
} | ||
|
||
// Usage: | ||
factorial(5); // Returns: 120 | ||
``` |
Mathys-Gasnier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: Fibonacci Number | ||
description: Calculates the nth Fibonacci number using recursion. | ||
tags: math, fibonacci, recursion | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int fibonacci(int n) { | ||
if (n <= 1) return n; | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
|
||
// Usage: | ||
fibonacci(6); // Returns: 8 | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: GCD | ||
description: Computes the greatest common divisor (GCD) of two integers. | ||
tags: math, gcd | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int gcd(int a, int b) { | ||
return b == 0 ? a : gcd(b, a % b); | ||
} | ||
|
||
// Usage: | ||
gcd(48, 18); // Returns: 6 | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: LCM | ||
description: Computes the least common multiple (LCM) of two integers. | ||
tags: math, lcm | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int lcm(int a, int b) { | ||
return (a / gcd(a, b)) * b; // Using GCD to calculate LCM | ||
} | ||
|
||
// Usage: | ||
lcm(12, 18); // Returns: 36 | ||
``` |
Mathys-Gasnier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Sum of Digits | ||
description: Calculates the sum of the digits of an integer. | ||
tags: math, digits | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int sum_of_digits(int n) { | ||
int sum = 0; | ||
while (n != 0) { | ||
sum += n % 10; | ||
n /= 10; | ||
} | ||
return sum; | ||
} | ||
|
||
// Usage: | ||
sum_of_digits(123); // Returns: 6 | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Binary Search | ||
description: Searches for a target value in a sorted array using binary search. | ||
tags: searching, binary-search | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int binary_search(const std::vector<int>& arr, int target) { | ||
int left = 0, right = arr.size() - 1; | ||
while (left <= right) { | ||
int mid = left + (right - left) / 2; // Avoids overflow | ||
if (arr[mid] == target) return mid; | ||
if (arr[mid] < target) | ||
left = mid + 1; | ||
else | ||
right = mid - 1; | ||
} | ||
return -1; // Element not found | ||
} | ||
|
||
// Usage: | ||
std::vector<int> nums = {1, 3, 5, 7, 9}; | ||
binary_search(nums, 7); // Returns: 3 (index of element) | ||
binary_search(nums, 2); // Returns: -1 (not found) | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Linear Search | ||
description: Searches for a target value in an array using linear search. | ||
tags: searching, linear-search | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
int linear_search(const std::vector<int>& arr, int target) { | ||
for (int i = 0; i < arr.size(); ++i) { | ||
if (arr[i] == target) return i; // Return index if found | ||
} | ||
return -1; // Element not found | ||
} | ||
|
||
// Usage: | ||
std::vector<int> nums = {4, 2, 8, 5, 1}; | ||
linear_search(nums, 5); // Returns: 3 (index of element) | ||
linear_search(nums, 10); // Returns: -1 (not found) | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Bubble Sort | ||
description: Sorts an array using the bubble sort algorithm. | ||
tags: sorting, bubble-sort | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
void bubble_sort(std::vector<int>& arr) { | ||
for (int i = 0; i < arr.size() - 1; ++i) { | ||
for (int j = 0; j < arr.size() - i - 1; ++j) { | ||
if (arr[j] > arr[j + 1]) { | ||
std::swap(arr[j], arr[j + 1]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Usage: | ||
std::vector<int> nums = {5, 3, 8, 6, 2}; | ||
bubble_sort(nums); // nums becomes: {2, 3, 5, 6, 8} | ||
``` |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Insertion Sort | ||
description: Sorts an array using the insertion sort algorithm. | ||
tags: sorting, insertion-sort | ||
author: ashukr07 | ||
--- | ||
|
||
```cpp | ||
void insertion_sort(std::vector<int>& arr) { | ||
for (int i = 1; i < arr.size(); ++i) { | ||
int key = arr[i]; | ||
int j = i - 1; | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
--j; | ||
} | ||
arr[j + 1] = key; | ||
} | ||
} | ||
|
||
// Usage: | ||
std::vector<int> nums = {12, 11, 13, 5, 6}; | ||
insertion_sort(nums); // nums becomes: {5, 6, 11, 12, 13} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.