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:
Getting Started with Custom Deserialization in Jackson
Spring Cloud AWS – S3
Guide to Guava Multimap
Case-Insensitive String Matching in Java
HttpClient 4 – Send Custom Cookie
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Guide to java.util.concurrent.Locks
Introduction to Apache Commons Text
Guide to @JsonFormat in Jackson
Simple Single Sign-On with Spring Security OAuth2
Disable DNS caching
Java Program to Implement the String Search Algorithm for Short Text Sizes
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Perform Search in a BST
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Spring Security OAuth Login with WebFlux
Runnable vs. Callable in Java
Life Cycle of a Thread in Java
Properties with Spring and Spring Boot
Java Program to find the peak element of an array using Binary Search approach
Java Program to Check whether Directed Graph is Connected using DFS
Hướng dẫn Java Design Pattern – Observer
Derived Query Methods in Spring Data JPA Repositories
Xử lý ngoại lệ trong Java (Exception Handling)
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Implement Attribute API
Inheritance with Jackson
Java Program to Find a Good Feedback Vertex Set
Comparing Arrays in Java
Giới thiệu về Stream API trong Java 8
Java Program to Implement Hash Tables chaining with Singly Linked Lists