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:
Spring Boot - Sending Email
Add Multiple Items to an Java ArrayList
ETags for REST with Spring
Fixing 401s with CORS Preflights and Spring Security
How to Round a Number to N Decimal Places in Java
Send email with JavaMail
Build a REST API with Spring and Java Config
Java Program to Implement Pagoda
Hướng dẫn sử dụng Java Annotation
Allow user:password in URL
Java – Try with Resources
Converting a Stack Trace to a String in Java
Giới thiệu Design Patterns
So sánh HashMap và HashSet trong Java
Partition a List in Java
Java Program to find the peak element of an array using Binary Search approach
Explain about URL and HTTPS protocol
Enum trong java
An Intro to Spring Cloud Vault
Java Program to Implement Hash Tables with Linear Probing
The Registration API becomes RESTful
Practical Java Examples of the Big O Notation
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Implement the Checksum Method for Small String Messages and Detect
Apache Commons Collections Bag
Mệnh đề if-else trong java
Custom JUnit 4 Test Runners
Java Program to Implement Hash Tables Chaining with Binary Trees
Send an email with an attachment
Quick Guide on Loading Initial Data with Spring Boot
Map to String Conversion in Java
Spring Boot - Tomcat Deployment