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:
Remove All Occurrences of a Specific Value from a List
Introduction to Spring Data JDBC
Kết hợp Java Reflection và Java Annotations
Filtering and Transforming Collections in Guava
Java – Try with Resources
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Caesar Cypher
Java Program to Construct an Expression Tree for an Postfix Expression
Java Concurrency Interview Questions and Answers
Creating a Web Application with Spring 5
Java Program to Implement Treap
A Guide To UDP In Java
Introduction to PCollections
Serve Static Resources with Spring
Spring Boot - Apache Kafka
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Find Nearest Neighbor for Static Data Set
Spring’s RequestBody and ResponseBody Annotations
Spring Cloud AWS – RDS
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement LinkedHashSet API
Java Program to Permute All Letters of an Input String
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Lớp Collections trong Java (Collections Utility Class)
A Guide to the Java ExecutorService
Java Program to Implement Efficient O(log n) Fibonacci generator
A Guide to Spring Cloud Netflix – Hystrix
Error Handling for REST with Spring
The StackOverflowError in Java
Java Timer
Spring REST API with Protocol Buffers