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:
Logout in an OAuth Secured Application
Introduction to Using FreeMarker in Spring MVC
Assertions in JUnit 4 and JUnit 5
Spring Boot - Zuul Proxy Server and Routing
Retrieve User Information in Spring Security
Java NIO2 Path API
Spring Security 5 for Reactive Applications
Comparing Strings in Java
JWT – Token-based Authentication trong Jersey 2.x
Spring REST API + OAuth2 + Angular
Remove the First Element from a List
Introduction to Spliterator in Java
Convert Hex to ASCII in Java
Java Program to Find the Edge Connectivity of a Graph
Java Program to Implement AttributeList API
Java Program to Implement Heap
Partition a List in Java
Jackson Date
Java Program to Implement Segment Tree
Java Program to Check whether Graph is a Bipartite using BFS
Java Web Services – JAX-WS – SOAP
Simple Single Sign-On with Spring Security OAuth2
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Send email with SMTPS (eg. Google GMail)
Sorting in Java
Custom JUnit 4 Test Runners
Testing an OAuth Secured API with Spring MVC
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement Flood Fill Algorithm
Sending Emails with Java
Apache Commons Collections OrderedMap
Finding the Differences Between Two Lists in Java