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:
Semaphore trong Java
Spring Boot - Actuator
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
A Guide to Apache Commons Collections CollectionUtils
Java Program to Create a Random Graph Using Random Edge Generation
Spring Security Custom AuthenticationFailureHandler
Java Program to Implement Booth Algorithm
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Versioning a REST API
Comparing Arrays in Java
Java – Rename or Move a File
What is a POJO Class?
An Intro to Spring Cloud Contract
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Implement PriorityBlockingQueue API
List Interface trong Java
Java Program to Implement the Program Used in grep/egrep/fgrep
Comparing Dates in Java
How to Read a Large File Efficiently with Java
Guide to DelayQueue
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Receive email using POP3
Guide to Escaping Characters in Java RegExps
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Hash Tables Chaining with List Heads
Guava Collections Cookbook
Java Program to Implement Counting Sort
Logout in an OAuth Secured Application
Introduction to Spring Data JPA
New Features in Java 9