Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cade4dd

Browse files
Merge pull request #217 from ashukr07/main
Added code snippets of C++ on bit manipulation, math & number, searching, sorting
2 parents 4a7a8b2 + 5a6d50b commit cade4dd

File tree

10 files changed

+191
-29
lines changed

10 files changed

+191
-29
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Check Power of Two
3+
description: Checks if a given number is a power of two using bitwise operations.
4+
tags: bit-manipulation, power-of-two
5+
author: ashukr07
6+
---
7+
8+
```c
9+
#include <stdbool.h> // Include the standard boolean library
10+
11+
bool is_power_of_two(int n) {
12+
return n > 0 && (n & (n - 1)) == 0; // Bitwise check for power of two
13+
}
14+
15+
// Usage:
16+
is_power_of_two(16); // Returns: true
17+
is_power_of_two(18); // Returns: false
18+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Reverse Bits
3+
description: Reverses the bits of a given unsigned integer.
4+
tags: bit-manipulation, reverse-bits
5+
author: ashukr07
6+
---
7+
8+
```c
9+
unsigned int reverse_bits(unsigned int n) {
10+
unsigned int result = 0;
11+
for (int i = 0; i < 32; ++i) {
12+
result <<= 1; // Shift result left by 1
13+
result |= n & 1; // Add the least significant bit of n to result
14+
n >>= 1; // Shift n right by 1
15+
}
16+
return result;
17+
}
18+
19+
// Usage:
20+
reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000)
21+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: XOR of Range
3+
description: Finds XOR of all numbers from 1 to n using properties of XOR.
4+
tags: bit-manipulation, xor
5+
author: ashukr07
6+
---
7+
8+
```c
9+
int xor_upto_n(int n) {
10+
if (n % 4 == 0) return n;
11+
if (n % 4 == 1) return 1;
12+
if (n % 4 == 2) return n + 1;
13+
return 0;
14+
}
15+
16+
// Usage:
17+
xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1)
18+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Check Perfect Number
3+
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).
4+
tags: math, perfect-number
5+
author: ashukr07
6+
---
7+
8+
```c
9+
#include <stdbool.h>
10+
11+
// Function to check if a number is a perfect number
12+
bool is_perfect(int n) {
13+
if (n <= 1) return false;
14+
15+
int sum = 1; // 1 is a divisor for all n > 1
16+
for (int i = 2; i * i <= n; ++i) {
17+
if (n % i == 0) {
18+
sum += i;
19+
if (i != n / i) sum += n / i;
20+
}
21+
}
22+
return sum == n;
23+
}
24+
25+
// Usage
26+
is_perfect(28); // Returns: true
27+
is_perfect(12); // Returns: false
28+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Compound Interest
3+
description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period.
4+
tags: math, finance
5+
author: ashukr07
6+
---
7+
8+
```c
9+
#include <math.h>
10+
11+
// Function to calculate compound interest
12+
double compound_interest(double principal, double rate, double time, int n) {
13+
return principal * pow(1 + rate / n, n * time);
14+
}
15+
16+
// Usage:
17+
double principal = 1000.0; // Initial amount
18+
double rate = 0.05; // Annual interest rate (5%)
19+
double time = 2; // Time in years
20+
int n = 4; // Compounded quarterly
21+
22+
compound_interest(principal, rate, time, n); // Returns: 1104.081632653061
23+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Fibonacci Number
3+
description: Calculates the nth Fibonacci number using recursion.
4+
tags: math, fibonacci, recursion
5+
author: ashukr07
6+
---
7+
8+
```c
9+
// Function to calculate the nth Fibonacci number
10+
int fibonacci(int n) {
11+
if (n <= 1) return n;
12+
return fibonacci(n - 1) + fibonacci(n - 2);
13+
}
14+
15+
// Usage:
16+
fibonacci(6); // Returns: 8
17+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Sum of Digits
3+
description: Calculates the sum of the digits of an integer.
4+
tags: math, digits
5+
author: ashukr07
6+
---
7+
8+
```c
9+
// Function to calculate the sum of the digits of an integer
10+
int sum_of_digits(int n) {
11+
int sum = 0;
12+
while (n != 0) {
13+
sum += n % 10;
14+
n /= 10;
15+
}
16+
return sum;
17+
}
18+
19+
// Usage:
20+
sum_of_digits(123); // Returns: 6
21+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Find Non-Repeating Number
3+
description: Finds the number that appears only once in an array where every other number appears twice.
4+
tags: bit-manipulation, xor
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int find_non_repeating(const std::vector<int>& nums) {
10+
int result = 0;
11+
for (int num : nums) {
12+
result ^= num;
13+
}
14+
return result;
15+
}
16+
17+
// Usage:
18+
std::vector<int> nums = {4, 1, 2, 1, 2};
19+
find_non_repeating(nums); // Returns: 4
20+
```

‎snippets/cpp/debuging/vector-print.md

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Binary to Decimal Conversion
3+
description: Converts a binary number represented as a string to its decimal equivalent.
4+
tags: binary, conversion
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int binary_to_decimal(const std::string& binary) {
10+
int decimal = 0;
11+
int base = 1; // Base value for the least significant bit
12+
13+
for (int i = binary.length() - 1; i >= 0; --i) {
14+
if (binary[i] == '1') {
15+
decimal += base;
16+
}
17+
base *= 2; // Move to the next power of 2
18+
}
19+
return decimal;
20+
}
21+
22+
// Usage:
23+
std::string binary = "1011"; // Binary representation of 11
24+
binary_to_decimal(binary); // Returns: 11
25+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /