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:
Beans and Dependency Injection
Introduction to Spring Cloud CLI
Java Program to Implement Max-Flow Min-Cut Theorem
Collect a Java Stream to an Immutable Collection
Generating Random Dates in Java
Validations for Enum Types
Partition a List in Java
Java IO vs NIO
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java 8 Stream findFirst() vs. findAny()
OAuth 2.0 Resource Server With Spring Security 5
Quick Guide to @RestClientTest in Spring Boot
Java 8 Predicate Chain
Hướng dẫn Java Design Pattern – Adapter
Java Program to Implement Stack
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Hướng dẫn Java Design Pattern – Dependency Injection
Java Program to Represent Graph Using Adjacency Matrix
Logging a Reactive Sequence
Merging Two Maps with Java 8
Luồng Daemon (Daemon Thread) trong Java
Java Program to subtract two large numbers using Linked Lists
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Spring Boot Security Auto-Configuration
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Solve the Fractional Knapsack Problem
Spring AMQP in Reactive Applications
Java Program to Implement AA Tree
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Implement Sorted Singly Linked List
Custom Cascading in Spring Data MongoDB