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:
Use Liquibase to Safely Evolve Your Database Schema
Introduction to Apache Commons Text
Testing in Spring Boot
List Interface trong Java
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Compact Strings in Java 9
Count Occurrences of a Char in a String
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
XML Serialization and Deserialization with Jackson
Biểu thức Lambda trong Java 8 – Lambda Expressions
Spring Security Custom AuthenticationFailureHandler
Iterating over Enum Values in Java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Spring Security 5 for Reactive Applications
Template Engines for Spring
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Implement PrinterStateReasons API
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Concrete Class in Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement Miller Rabin Primality Test Algorithm
Send an email using the SMTP protocol
Guide to the Synchronized Keyword in Java
Jackson Annotation Examples
Spring WebClient Requests with Parameters
How to Get All Dates Between Two Dates?
Spring Security OAuth2 – Simple Token Revocation