Java Program to Implement Bit Array

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 Implement the String Search Algorithm for Short Text Sizes
Java 8 Collectors toMap
Java Program to Find Nearest Neighbor for Dynamic Data Set
Spring Boot - Application Properties
Java Program to Implement Hash Tables with Linear Probing
Spring RestTemplate Request/Response Logging
Pagination and Sorting using Spring Data JPA
Cơ chế Upcasting và Downcasting trong java
The Spring @Controller and @RestController Annotations
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Spring Boot - Exception Handling
Getting Started with Forms in Spring MVC
Introduction to Spring Cloud CLI
Java Program to find the peak element of an array using Binary Search approach
A Guide to JUnit 5
Summing Numbers with Java Streams
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Guide to Character Encoding
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Implement Sieve Of Sundaram
The “final” Keyword in Java
Optional trong Java 8
Java Program to Implement Adjacency List
An Introduction to ThreadLocal in Java
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Comparing Two HashMaps in Java
Java Program to Implement Sieve Of Eratosthenes
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Join and Split Arrays and Collections in Java
Java Program to Implement String Matching Using Vectors