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:
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Chuyển đổi từ HashMap sang ArrayList
Hướng dẫn Java Design Pattern – Chain of Responsibility
Spring Boot - Cloud Configuration Server
Check if there is mail waiting
Add Multiple Items to an Java ArrayList
Java Program to Implement AA Tree
Java Program to Permute All Letters of an Input String
Using JWT with Spring Security OAuth (legacy stack)
Java Program to implement Associate Array
Spring Boot - Rest Controller Unit Test
Hướng dẫn sử dụng Java Annotation
Java Program to Implement ScapeGoat Tree
Introduction to Spring Method Security
Logout in an OAuth Secured Application
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Từ khóa static và final trong java
A Guide to Apache Commons Collections CollectionUtils
Java Program to Perform Right Rotation on a Binary Search Tree
Introduction to PCollections
OAuth2.0 and Dynamic Client Registration
Check If a File or Directory Exists in Java
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Implement Control Table
Java Program to Implement Max Heap
Overflow and Underflow in Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Logout in an OAuth Secured Application
Java Program to Implement Find all Forward Edges in a Graph
Retrieve User Information in Spring Security
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Perform Sorting Using B-Tree