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:
Mapping a Dynamic JSON Object with Jackson
Immutable Map Implementations in Java
Java Program to Perform Finite State Automaton based Search
Date Time trong Java 8
Converting String to Stream of chars
SOAP Web service: Authentication trong JAX-WS
Introduction to Spring Data JDBC
Control Structures in Java
Explain about URL and HTTPS protocol
Java 8 Predicate Chain
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Easy Ways to Write a Java InputStream to an OutputStream
Hướng dẫn Java Design Pattern – Null Object
Apache Commons Collections OrderedMap
Introduction to Java 8 Streams
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Collections Interview Questions
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Implement the Bin Packing Algorithm
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Solve any Linear Equations
Static Content in Spring WebFlux
Assertions in JUnit 4 and JUnit 5
Java Program to Implement Ternary Search Algorithm
Generate Spring Boot REST Client with Swagger
Java Program to Implement Counting Sort
Java Program to Implement Bloom Filter
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Testing an OAuth Secured API with Spring MVC
Spring Security – security none, filters none, access permitAll
Spring Security Authentication Provider