From 453505acac5bef7f7d5a94bc0f509ade38f7b2b8 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 11:18:59 -0500 Subject: [PATCH 1/4] add: HammingWeightsOfIntegers --- .../HammingWeightsOfIntegers.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 java/Bit Manipulation/HammingWeightsOfIntegers.java diff --git a/java/Bit Manipulation/HammingWeightsOfIntegers.java b/java/Bit Manipulation/HammingWeightsOfIntegers.java new file mode 100644 index 0000000..c781853 --- /dev/null +++ b/java/Bit Manipulation/HammingWeightsOfIntegers.java @@ -0,0 +1,23 @@ +import java.util.Arrays; + +public class HammingWeightsOfIntegers { + public hammingWeightsOfIntegers(int n) { + int[] res = new int[n + 1]; + for (int i = 0; i < n + 1; i++) { + res[i] = countSetBits(i); + } + return res; + } + + private int countSetBits(int x) { + int count = 0; + // Count each set bit of 'x' until 'x' equals 0. + while (x > 0) { + // Increment the count if the LSB is 1. + count += x & 1; + // Right shift 'x' to shift the next bit to the LSB position. + x >>= 1; + } + return count; + } +} From e05741647f61829ffb2823e0eb159bbe4ee3b74f Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 11:22:23 -0500 Subject: [PATCH 2/4] add: HammingWeightsOfIntegersDp and fix minor error in HammingWeightsOfIntegers --- java/Bit Manipulation/HammingWeightsOfIntegers.java | 8 +++----- .../HammingWeightsOfIntegersDp.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 java/Bit Manipulation/HammingWeightsOfIntegersDp.java diff --git a/java/Bit Manipulation/HammingWeightsOfIntegers.java b/java/Bit Manipulation/HammingWeightsOfIntegers.java index c781853..4227434 100644 --- a/java/Bit Manipulation/HammingWeightsOfIntegers.java +++ b/java/Bit Manipulation/HammingWeightsOfIntegers.java @@ -1,10 +1,8 @@ -import java.util.Arrays; - public class HammingWeightsOfIntegers { - public hammingWeightsOfIntegers(int n) { + public int[] hammingWeightsOfIntegers(int n) { int[] res = new int[n + 1]; - for (int i = 0; i < n + 1; i++) { - res[i] = countSetBits(i); + for (int x = 0; x < n + 1; x++) { + res[x] = countSetBits(x); } return res; } diff --git a/java/Bit Manipulation/HammingWeightsOfIntegersDp.java b/java/Bit Manipulation/HammingWeightsOfIntegersDp.java new file mode 100644 index 0000000..63793d7 --- /dev/null +++ b/java/Bit Manipulation/HammingWeightsOfIntegersDp.java @@ -0,0 +1,13 @@ +public class HammingWeightsOfIntegersDp { + public int[] hammingWeightsOfIntegersDp(int n) { + // Base case: the number of set bits in 0 is just 0. We set dp[0] to + // 0 by initializing the entire DP array to 0. + int[] dp = new int[n + 1]; + for (int x = 0; x < n + 1; x++) { + // 'dp[x]' is obtained using the result of 'dp[x >> 1]', plus + // the LSB of 'x'. + dp[x] = dp[x >> 1] + (x & 1); + } + return dp; + } +} From 9655bef4d91e4c60eed01493075dd496c5f2c1a9 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 11:25:22 -0500 Subject: [PATCH 3/4] add: LonelyInteger --- java/Bit Manipulation/LonelyInteger.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 java/Bit Manipulation/LonelyInteger.java diff --git a/java/Bit Manipulation/LonelyInteger.java b/java/Bit Manipulation/LonelyInteger.java new file mode 100644 index 0000000..aa81f29 --- /dev/null +++ b/java/Bit Manipulation/LonelyInteger.java @@ -0,0 +1,13 @@ +public class LonelyInteger { + public int lonelyInteger(int[] nums) { + int res = 0; + // XOR each element of the array so that duplicate values will + // cancel each other out (x ^ x == 0). + for (int num : nums) { + res ^= num; + } + // 'res' will store the lonely integer because it would not have + // been canceled out by any duplicate. + return res; + } +} From 1c5fcfd1b64e60ce2d5a48f2e8352ee8af4538e8 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Thu, 6 Feb 2025 11:32:37 -0500 Subject: [PATCH 4/4] add: SwapOddAndEvenBits --- java/Bit Manipulation/SwapOddAndEvenBits.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 java/Bit Manipulation/SwapOddAndEvenBits.java diff --git a/java/Bit Manipulation/SwapOddAndEvenBits.java b/java/Bit Manipulation/SwapOddAndEvenBits.java new file mode 100644 index 0000000..3530140 --- /dev/null +++ b/java/Bit Manipulation/SwapOddAndEvenBits.java @@ -0,0 +1,11 @@ +public class SwapOddAndEvenBits { + public int swapOddAndEvenBits(int n) { + int evenMask = 0x55555555; // 01010101010101010101010101010101 + int oddMask = 0xAAAAAAAA; // 10101010101010101010101010101010 + int evenBits = n & evenMask; + int oddBits = n & oddMask; + // Shift the even bits to the left, the odd bits to the right, and + // merge these shifted values together. + return (evenBits << 1) | (oddBits >> 1); + } +}