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 Encode a Message Using Playfair Cipher
Java Concurrency Interview Questions and Answers
An Intro to Spring Cloud Zookeeper
4 tính chất của lập trình hướng đối tượng trong Java
Read an Outlook MSG file
Guide to @JsonFormat in Jackson
Java Program to Implement Adjacency List
Hướng dẫn Java Design Pattern – Interpreter
Jackson – Change Name of Field
Get the workstation name or IP
Display Auto-Configuration Report in Spring Boot
Inheritance with Jackson
Java Program to Implement D-ary-Heap
DistinctBy in the Java Stream API
Java – File to Reader
Hướng dẫn Java Design Pattern – DAO
Spring 5 Testing with @EnabledIf Annotation
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Implement LinkedTransferQueue API
Compare Two JSON Objects with Jackson
Updating your Password
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement LinkedHashSet API
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Spring RequestMapping
Introduction to Liquibase Rollback
Java Program to Implement Find all Forward Edges in a Graph
Encode a String to UTF-8 in Java
Java 8 Collectors toMap
Java Program to Implement LinkedBlockingDeque API
File Upload with Spring MVC
TreeSet và sử dụng Comparable, Comparator trong java