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 Check the Connectivity of Graph Using DFS
Guide to java.util.concurrent.Future
Java Program for Topological Sorting in Graphs
Tính đóng gói (Encapsulation) trong java
Overview of Spring Boot Dev Tools
Registration – Password Strength and Rules
The Order of Tests in JUnit
Spring Security and OpenID Connect
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Perform Quick Sort on Large Number of Elements
Spring Cloud – Adding Angular
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Implement Insertion Sort
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Check the Connectivity of Graph Using BFS
Check If Two Lists are Equal in Java
New Features in Java 13
Spring Boot - Quick Start
Annotation trong Java 8
Tạo số và chuỗi ngẫu nhiên trong Java
Spring Boot - Introduction
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Apache Camel with Spring Boot
Sử dụng CountDownLatch trong Java
Limiting Query Results with JPA and Spring Data JPA
Introduction to Java 8 Streams
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
A Comparison Between Spring and Spring Boot
Command-Line Arguments in Java
Java Program to Find the Mode in a Data Set
Java – Byte Array to Reader