This Java program is to Implement Bit Array. A bit array (also known as bitmap, bitset, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level parallelism in hardware to perform operations quickly.
Here is the source code of the Java program to implement bit array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.util.BitSet;
public class BitArray
{
private BitSet bits;
public BitArray(String bits)
{
this.bits = fromString(bits);
}
private BitSet getBitSet()
{
return bits;
}
private void setBitSet(BitSet bitSet )
{
bits = bitSet;
}
public BitArray and(BitArray bitarray)
{
BitSet bits1 = this.getBitSet();
BitSet bits2 = bitarray.getBitSet();
bits1.and(bits2);
this.setBitSet(bits1);
return this;
}
public BitArray or(BitArray bitarray)
{
BitSet bits1 = this.getBitSet();
BitSet bits2 = bitarray.getBitSet();
bits1.or(bits2);
this.setBitSet(bits1);
return this;
}
private BitSet fromString(String bit)
{
return BitSet.valueOf(new long[] { Long.parseLong(bit, 2) });
}
public String toString()
{
return Long.toString(bits.toLongArray()[0], 2);
}
public static void main (String...arg)
{
BitArray array1 = new BitArray("1010");
BitArray array2 = new BitArray("1001");
BitArray array3 = new BitArray("1100");
System.out.println("The BitArray Are");
System.out.println("First :" + array1);
System.out.println("Second :" +array2);
System.out.println("Third : " + array3);
System.out.println("First AND Second");
System.out.println(array1.and(array2));
System.out.println("Second OR Third");
System.out.println(array2.or(array3));
}
}
$javac BitArray.java $java BitArray The BitArray Are First :1010 Second :1001 Third : 1100 First AND Second 1000 Second OR Third 1101
Related posts:
Java Program to Implement Vector API
Hướng dẫn Java Design Pattern – Prototype
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement Insertion Sort
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Converting a List to String in Java
Java – Rename or Move a File
Spring Boot: Customize the Jackson ObjectMapper
Hướng dẫn Java Design Pattern – Iterator
HttpAsyncClient Tutorial
Java Program to Implement Knight’s Tour Problem
Notify User of Login From New Device or Location
Map Serialization and Deserialization with Jackson
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Chương trình Java đầu tiên
Java Program to Implement Dijkstra’s Algorithm using Queue
A Quick Guide to Spring MVC Matrix Variables
Java Program to Implement Miller Rabin Primality Test Algorithm
Spring Boot - Tracing Micro Service Logs
Period and Duration in Java
Java Program to Implement Naor-Reingold Pseudo Random Function
Spring REST API with Protocol Buffers
Lấy ngày giờ hiện tại trong Java
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement Knapsack Algorithm
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Perform Polygon Containment Test
Serve Static Resources with Spring
Composition, Aggregation, and Association in Java
Java – Try with Resources
Multipart Upload with HttpClient 4
Java Program to Implement Hamiltonian Cycle Algorithm