-
-
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 all 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,18 @@ | ||
--- | ||
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 | ||
--- | ||
|
||
```c | ||
#include <stdbool.h> // Include the standard boolean library | ||
|
||
bool is_power_of_two(int n) { | ||
return n > 0 && (n & (n - 1)) == 0; // Bitwise check for power of two | ||
} | ||
|
||
// 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,21 @@ | ||
--- | ||
title: Reverse Bits | ||
description: Reverses the bits of a given unsigned integer. | ||
tags: bit-manipulation, reverse-bits | ||
author: ashukr07 | ||
--- | ||
|
||
```c | ||
unsigned int reverse_bits(unsigned int n) { | ||
unsigned int result = 0; | ||
for (int i = 0; i < 32; ++i) { | ||
result <<= 1; // Shift result left by 1 | ||
result |= n & 1; // Add the least significant bit of n to result | ||
n >>= 1; // Shift n right by 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 | ||
--- | ||
|
||
```c | ||
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 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,28 @@ | ||
--- | ||
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 | ||
--- | ||
|
||
```c | ||
#include <stdbool.h> | ||
|
||
// Function to check if a number is a perfect number | ||
bool is_perfect(int n) { | ||
if (n <= 1) return false; | ||
|
||
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; | ||
} | ||
|
||
// Usage | ||
is_perfect(28); // Returns: true | ||
is_perfect(12); // 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,23 @@ | ||
--- | ||
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 | ||
--- | ||
|
||
```c | ||
#include <math.h> | ||
|
||
// Function to calculate compound interest | ||
double compound_interest(double principal, double rate, double time, int n) { | ||
return principal * 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 | ||
``` |
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,17 @@ | ||
--- | ||
title: Fibonacci Number | ||
description: Calculates the nth Fibonacci number using recursion. | ||
tags: math, fibonacci, recursion | ||
author: ashukr07 | ||
--- | ||
|
||
```c | ||
// Function to calculate the nth Fibonacci number | ||
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,21 @@ | ||
--- | ||
title: Sum of Digits | ||
description: Calculates the sum of the digits of an integer. | ||
tags: math, digits | ||
author: ashukr07 | ||
--- | ||
|
||
```c | ||
// Function to calculate the sum of the digits of an integer | ||
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 | ||
``` |
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 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 | ||
``` |
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.