How to find hamming weight in java
May 08, 2024
Hammingweight
Table of Content
How to find hamming weight in Java
hamming weight for a number is the count of bits that are non zero. For instance for 1001 hamming weight is 2. For 100001111 hamming weight is 5. In this article we will see how to find hamming weight efficiently.
Using Simple divide and reminder
Here we are divide the number by 2 and until it becomes 0 and each step we check if the intermediate gives reminder 1 while dividing by 2.
public static int hammingWeight(int n) {
int count = 0;
while (n != 0) {
if (n % 2 == 1) {
count++;
}
n = n / 2;
}
return count;
}
Using Bit marking
In this example we are using Bit masking. Since the input is an Integer and it contains 32 bits. We do a & (bit wise and) operation for each of its digits.
public static int hammingWeightII(int n) {
int count = 0;
int mask = 1;
for (int i = 0; i <= 31; i++) {
count += (n & mask) == 0 ? 0 : 1;
//expand the mask
mask = mask << 1;
}
return count;
}
Full Example
package ic.binary;
public class NumberOf1s {
public static void main(String[] args) {
int n = 5;
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
n = 8;
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
}
public static int hammingWeight(int n) {
int count = 0;
while (n != 0) {
if (n % 2 == 1) {
count++;
}
n = n / 2;
}
return count;
}
public static int hammingWeightII(int n) {
int count = 0;
int mask = 1;
for (int i = 0; i <= 31; i++) {
count += (n & mask) == 0 ? 0 : 1;
mask = mask << 1;
}
return count;
}
}
Output of above program
Number of 1 bits for :5 -> 2 Number of 1 bits for :5 -> 2 Number of 1 bits for :8 -> 1 Number of 1 bits for :8 -> 1
</
Table of Content
Java OptionalInt example
OptionalInt allows us to create an object which may or may not contain a int value. If a value is present, isPresent() will return true and getAsInt() will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse().
Other classes similar to OptionalInt are OptionalFloat, OptionalDouble, Optional. These can help us eliminate exceptions that occur due to the absence of a value at runtime. Basically we need to first check if the Optional is carrying any value then only try to get value.
In this example we are returning an OptionalInt from Stream created from integer array and finally returning the sum using reduce method. If the Value is present then only we are trying to pring the value by calling result.getAsint()
package javaexp.blogspot.stream;
import java.util.Arrays;
import java.util.OptionalInt;
public class OptionalIntExample {
public static void main(String[] args) {
int iarray[] = {9, 10, 11, 12, 15, 15, 25};
OptionalInt result = Arrays.stream(iarray).reduce((left, right) ->left );
if (result.isPresent() ) {
System.out.println("Sum of Array " + result.getAsInt());
}
}
}
Summary
OptionalInt and other respective Optional classes helping in protecting from Nullpointer exception when we try to get value (say integer value) from and Integer object which is null.
Bucket sort implementation in Java
May 05, 2024
Sorting
Table of Content
Bucket Sort in Java
In this article we will go though a simple implementation of Bucket sort in Java
What is Bucket sort
Bucket sort is a sorting algorithm that divides the inputs into several buckets. Once the buckets are populated with input data, then all these buckets are sorted individually using a different sorting mechanism. After individual buckets are sorted they are contaminated together and returned as final result.
Following is the Pseudocode for Bucket sort