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 Implement String Matching Using Vectors
An Intro to Spring Cloud Vault
HashSet trong Java hoạt động như thế nào?
Java Program to Generate Date Between Given Range
How to Break from Java Stream forEach
Spring Boot - OAuth2 with JWT
Deploy a Spring Boot WAR into a Tomcat Server
Most commonly used String methods in Java
Immutable Map Implementations in Java
Java Program to Implement Dijkstra’s Algorithm using Queue
Spring @RequestParam Annotation
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Request Method Not Supported (405) in Spring
Spring Boot - Database Handling
Guide to Apache Commons CircularFifoQueue
Spring WebClient Filters
Test a REST API with Java
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
The Java 8 Stream API Tutorial
Guide to Guava Multimap
Receive email by java client
Prevent Brute Force Authentication Attempts with Spring Security
Using JWT with Spring Security OAuth
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Implement Vector API
ExecutorService – Waiting for Threads to Finish
Java Program to Implement D-ary-Heap
Interface trong Java 8 – Default method và Static method
Java Program to Implement Skip List
Jackson Unmarshalling JSON with Unknown Properties
Spring Security – Reset Your Password
Use Liquibase to Safely Evolve Your Database Schema