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 Decode a Message Encoded Using Playfair Cipher
Java Program to Implement Stack using Two Queues
Java – Generate Random String
Thao tác với tập tin và thư mục trong Java
RegEx for matching Date Pattern in Java
Java Program to Check Cycle in a Graph using Topological Sort
A Guide to JUnit 5
Java Program to Implement Repeated Squaring Algorithm
A Guide to BitSet in Java
Guide to the Fork/Join Framework in Java
Getting Started with Custom Deserialization in Jackson
Hashing a Password in Java
Control the Session with Spring Security
Java Program to implement Bi Directional Map
A Guide to JPA with Spring
Java Program to Perform Polygon Containment Test
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Server-Sent Events in Spring
Java Program to Implement vector
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement VList
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Registration with Spring Security – Password Encoding
An Intro to Spring Cloud Security
HttpClient Connection Management
Upload and Display Excel Files with Spring MVC
Java Program to Create a Random Linear Extension for a DAG
Spring Data JPA and Null Parameters
HttpClient 4 – Follow Redirects for POST
Java Program to Check for balanced parenthesis by using Stacks
Java equals() and hashCode() Contracts