Skip to content

Commit 181be5c

Browse files
Merge pull request #230 from Mcbencrafter/main
Added math and bit manipulation snippets for java
2 parents 88d7805 + f5ff9b7 commit 181be5c

12 files changed

+190
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Bit Counting
3+
description: Counts the set bits in the binary representation of an integer
4+
author: Mcbencrafter
5+
tags: math,number,bits,bit-counting
6+
---
7+
8+
```java
9+
public static int countBits(int number) {
10+
int bits = 0;
11+
12+
while (number > 0) {
13+
bits += number & 1;
14+
number >>= 1;
15+
}
16+
17+
return bits;
18+
}
19+
20+
// Usage:
21+
int number = 5;
22+
System.out.println(countBits(5)); // 2 (101)
23+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Is Power Of Two
3+
description: Checks if a number is a power of two
4+
author: Mcbencrafter
5+
tags: math,number,bit,power-of-two
6+
---
7+
8+
```java
9+
public static boolean isPowerOfTwo(int number) {
10+
return (number > 0) && ((number & (number - 1)) == 0);
11+
}
12+
13+
// Usage:
14+
int number = 16;
15+
System.out.println(isPowerOfTwo(5)); // true (2^4)
16+
```

snippets/java/date-time/date-time-formatting-american.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Date time formatting american
2+
title: Date Time Formatting American
33
description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a"
44
author: Mcbencrafter
55
tags: date,time,date-time,formatting,american

snippets/java/date-time/date-time-formatting-european.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Date time formatting european
2+
title: Date Time Formatting European
33
description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss"
44
author: Mcbencrafter
55
tags: date,time,date-time,formatting,european

snippets/java/date-time/duration-formatting-hours-minutes-seconds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Duration formatting hours minutes seconds
2+
title: Duration Formatting Hours Minutes Seconds
33
description: Converts a given time duration to a human-readable string in the format "hh:mm(:ss)"
44
author: Mcbencrafter
55
tags: time,formatting,hours,minutes,seconds

snippets/java/date-time/duration-formatting-minutes-seconds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Duration formatting minutes seconds
2+
title: Duration Formatting Minutes Seconds
33
description: Converts a given time duration to a human-readable string in the format "mm:ss"
44
author: Mcbencrafter
55
tags: time,formatting,minutes,seconds

snippets/java/math/checksum.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Checksum
3+
description: Calculates the checksum of an int
4+
author: Mcbencrafter
5+
tags: math,number,checksum
6+
---
7+
8+
```java
9+
public static int checksum(int number) {
10+
number = Math.abs(number);
11+
int sum = 0;
12+
13+
while (number != 0) {
14+
sum += number % 10;
15+
number /= 10;
16+
}
17+
18+
return sum;
19+
}
20+
21+
// Usage:
22+
int number = 12345;
23+
System.out.println(checksum(number)); // 15 = 1+2+3+4+5
24+
```

snippets/java/math/factorial.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Factorial
3+
description: Computes the factorial of a given number
4+
author: Mcbencrafter
5+
tags: math,number,factorial
6+
---
7+
8+
```java
9+
import java.math.BigInteger;
10+
11+
public static BigInteger factorial(int number) {
12+
BigInteger result = BigInteger.ONE;
13+
14+
for (int currentNumber = 1; currentNumber <= number; currentNumber++) {
15+
result = result.multiply(BigInteger.valueOf(currentNumber));
16+
}
17+
18+
return result;
19+
}
20+
21+
// Usage:
22+
int number = 6;
23+
System.out.println(factorial(number)); // 720 = 6*5*4*3*2
24+
```

snippets/java/math/fibonacci.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Fibonacci
3+
description: Calculates the nth fibonacci number
4+
author: Mcbencrafter
5+
tags: math,number,fibonacci
6+
---
7+
8+
```java
9+
public static int fibonacci(int number) {
10+
if (number <= 1)
11+
return number;
12+
13+
return fibonacci(number - 1) + fibonacci(number - 2);
14+
}
15+
16+
// Usage:
17+
int number = 5;
18+
System.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3)
19+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Greatest Common Divisor
3+
description: Calculates the greatest common divisor (gcd) of two numbers
4+
author: Mcbencrafter
5+
tags: math,number,greatest-common-devisor,gcd,euclidean-algorithm
6+
---
7+
8+
```java
9+
public static int gcd(int number1, int number2) {
10+
while (number2 != 0) {
11+
int remainder = number2;
12+
number2 = number1 % number2;
13+
number1 = remainder;
14+
}
15+
16+
return number1;
17+
}
18+
19+
// Usage:
20+
int a = 16;
21+
int b = 12;
22+
System.out.println(gcd(a, b)); // 4
23+
```

0 commit comments

Comments
 (0)