Sign in

    immut_BitVector

    An efficient immutable bit vector data structure for compact and efficient storage and manipulation of boolean sequences, supporting bit operations, logical operations, slicing, concatenation and more, while maintaining immutability

    bit-vector
    immutable
    bit-manipulation
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    30

    #immut_BitVector

    immut_BitVector is an efficient immutable bit vector data structure for compact and efficient storage and manipulation of boolean sequences. It provides a series of bit operations while maintaining immutability (all modification operations return new immut_BitVector instances).

    #🚀 Key Features

    â€ĸ 🔄 Multiple Construction Methods - Support for creating empty vectors, all-0 vectors, all-1 vectors, and creating from boolean arrays, integer arrays, or strings â€ĸ ➕ Basic Bit Operations - Get, set, clear, and flip bits at specific positions â€ĸ 🔀 Logical Operations - Support for bitwise AND, OR, XOR, and other operations â€ĸ đŸ”ĸ Bit Counting and Finding - Count the number of 1s and 0s, find the first set bit â€ĸ âœ‚ī¸ Slicing and Concatenation - Extract sub-vectors and combine vectors â€ĸ 📊 Serialization - Convert to string or boolean array â€ĸ đŸ› ī¸ Utility Methods - Check if all bits are 0, all bits are 1, and other practical functions

    #đŸ“Ĩ Installation

    moon add kesmeey/immut_BitVector

    #🚀 Usage Guide

    #🔨 Creating Bit Vectors

    You can create bit vectors using the new(), ones(), from_bools(), from_ints(), or from_string() methods.

    // Create a bit vector with all bits set to 0
    let bv1 = @immut_BitVector.new(10) // Length 10, all 0s

    // Create a bit vector with all bits set to 1
    let bv2 = @immut_BitVector.ones(10) // Length 10, all 1s

    // Create from boolean array
    let bools = FixedArray::make(5, false)
    bools[1] = true
    bools[3] = true
    let bv3 = @immut_BitVector.from_bools(bools) // Results in "01010"

    // Create from string
    let bv4 = @immut_BitVector.from_string("10110") // From binary string

    // Create from integer array
    let ints = FixedArray::make(2, (0).to_uint64())
    ints[0] = (42).to_uint64() // Binary: ...0101010
    let bv5 = @immut_BitVector.from_ints(ints, 128) // 128-bit length vector

    #➕ Basic Bit Operations

    Use the get(), set(), clear(), and flip() methods to manipulate bits at specific positions.

    let bv = @immut_BitVector.new(10) // Create all-0 bit vector

    // Get bit value
    let bit = bv.get(3) // Get bit value at index 3, returns false

    // Set bit (returns new bit vector)
    let bv2 = bv.set(3) // Set bit at index 3 to 1
    assert_eq!(bv2.get(3), true)
    assert_eq!(bv.get(3), false) // Original vector unchanged

    // Clear bit
    let bv3 = bv2.clear(3) // Clear bit at index 3 to 0
    assert_eq!(bv3.get(3), false)

    // Flip bit
    let bv4 = bv.flip(5) // Flip bit at index 5
    assert_eq!(bv4.get(5), true)

    // Flip all bits
    let bv5 = bv.flip_all() // Flip all bits
    assert_eq!(bv5.get(0), true)

    #🔀 Logical Operations

    Use the and(), or(), xor(), and not() methods for bitwise logical operations.

    let bv1 = @immut_BitVector.from_string("1100")
    let bv2 = @immut_BitVector.from_string("1010")

    // Bitwise AND
    let and_result = bv1.and(bv2) // "1000"

    // Bitwise OR
    let or_result = bv1.or(bv2) // "1110"

    // Bitwise XOR
    let xor_result = bv1.xor(bv2) // "0110"

    // Bitwise NOT (flip all bits)
    let not_result = bv1.not() // "0011"

    #đŸ”ĸ Bit Counting and Finding

    Use the count_ones(), count_zeros(), and find_first_set() methods to count and find bits.

    let bv = @immut_BitVector.from_string("101100")

    // Count the number of 1s
    let ones = bv.count_ones() // 3

    // Count the number of 0s
    let zeros = bv.count_zeros() // 3

    // Find the position of the first bit set to 1
    let first_set = bv.find_first_set() // 0 (zero-based index)

    #âœ‚ī¸ Slicing and Concatenation

    Use the slice() and concat() methods to extract sub-vectors and combine vectors.

    let bv = @immut_BitVector.from_string("1011001010")

    // Slice (inclusive start, exclusive end)
    let slice = bv.slice(2, 6) // "1100"

    // Concatenate two bit vectors
    let bv2 = @immut_BitVector.from_string("0101")
    let concat_result = bv.concat(bv2) // "10110010100101"

    #📊 Serialization

    Use the to_string() and to_bools() methods to convert bit vectors to other formats.

    let bv = @immut_BitVector.from_string("10110")

    // Convert to string
    let str = bv.to_string() // "10110"

    // Convert to boolean array
    let bools = bv.to_bools() // [true, false, true, true, false]

    #đŸ› ī¸ Utility Methods

    Use the is_all_zeros(), is_all_ones(), and length() methods to get the status of bit vectors.

    let bv1 = @immut_BitVector.new(10) // All-0 bit vector
    let bv2 = @immut_BitVector.ones(10) // All-1 bit vector

    // Check if all bits are 0
    assert_true!(bv1.is_all_zeros())
    assert_false!(bv2.is_all_zeros())

    // Check if all bits are 1
    assert_true!(bv2.is_all_ones())
    assert_false!(bv1.is_all_ones())

    // Get length
    assert_eq!(bv1.length(), 10)

    // Create bit masks
    let mask = @immut_BitVector.bit_mask(3) // Create mask at position 3 (...0001000)
    let low_mask = @immut_BitVector.low_bits_mask(3) // Create mask from 0 to position 3 (...0001111)

    #🚀 Advanced Usage Examples

    #Example: Implementing Simple Sets with BitVector

    // Use BitVector to represent sets containing elements 0-9
    let set_a = @immut_BitVector.from_string("1010100101") // Contains elements 0,2,4,7,9
    let set_b = @immut_BitVector.from_string("1100011001") // Contains elements 0,3,4,5,8,9

    // Set operations
    let union = set_a.or(set_b) // Union
    let intersection = set_a.and(set_b) // Intersection
    let difference = set_a.xor(set_b) // Symmetric difference

    // Print results
    println("Set A: " + set_a.to_string()) // 1010100101
    println("Set B: " + set_b.to_string()) // 1100011001
    println("Union: " + union.to_string()) // 1110111101
    println("Intersection: " + intersection.to_string()) // 1000000001
    println("Symmetric Difference: " + difference.to_string()) // 0110111100

    #Example: Efficient Bit Operations with BitVector

    // Create a 128-bit vector for bit field operations
    let flags = @immut_BitVector.new(128)

    // Set different flag bits
    let flags = flags.set(5).set(10).set(42).set(100)

    // Check flag bits
    if flags.get(10) {
    println("Flag 10 is set")
    }

    // Use masks for batch operations
    let mask = @immut_BitVector.from_string("00111100") // Bit mask
    let masked = flags.slice(0, 8).and(mask) // Apply mask to first 8 bits

    println("After applying mask: " + masked.to_string())

    #📜 License

    This project is licensed under the Apache-2.0 License. See LICENSE file for details.

    #đŸ“ĸ Contact & Support

    â€ĸ Moonbit Community: moonbit-community â€ĸ GitHub Issues: Report an Issue

    👋 If you like this project, give it a ⭐! Happy coding! 🚀

    BitVector

    pub(all) struct BitVector {
    data : FixedArray[UInt64]
    length : Int
    last_mask : UInt64
    }

    impl Eq for BitVector
    impl Show for BitVector

    BitVector::and

    fn BitVector::and(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise AND operation on bit vectors, returns a new bit vector

    BitVector::clear

    fn BitVector::clear(self : BitVector, index : Int) -> BitVector

    Clears the bit at the specified position to 0, returns a new bit vector

    BitVector::concat

    fn BitVector::concat(self : BitVector, other : BitVector) -> BitVector

    Concatenates two bit vectors, returns a new bit vector

    BitVector::count_ones

    fn BitVector::count_ones(self : BitVector) -> Int

    Counts the number of 1s in the bit vector

    BitVector::count_zeros

    fn BitVector::count_zeros(self : BitVector) -> Int

    Counts the number of 0s in the bit vector

    BitVector::find_first_set

    fn BitVector::find_first_set(self : BitVector) -> Int

    Finds the index of the first bit set to 1 in the vector, returns -1 if none

    BitVector::flip

    fn BitVector::flip(self : BitVector, index : Int) -> BitVector

    Flips the value of the bit at the specified position, returns a new bit vector

    BitVector::flip_all

    fn BitVector::flip_all(self : BitVector) -> BitVector

    Flips all bits in the vector, returns a new bit vector

    BitVector::get

    fn BitVector::get(self : BitVector, index : Int) -> Bool

    Gets the boolean value at the specified position in the bit vector

    BitVector::is_all_ones

    fn BitVector::is_all_ones(self : BitVector) -> Bool

    Checks if the bit vector is all ones

    BitVector::is_all_zeros

    fn BitVector::is_all_zeros(self : BitVector) -> Bool

    Checks if the bit vector is all zeros

    BitVector::length

    fn BitVector::length(self : BitVector) -> Int

    Gets the length of the bit vector

    BitVector::not

    fn BitVector::not(self : BitVector) -> BitVector

    BitVector::or

    fn BitVector::or(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise OR operation on bit vectors, returns a new bit vector

    BitVector::set

    fn BitVector::set(self : BitVector, index : Int) -> BitVector

    Sets the bit at the specified position to 1, returns a new bit vector

    BitVector::slice

    fn BitVector::slice(self : BitVector, start : Int, end : Int) -> BitVector

    Extracts a sub-vector from the bit vector, starting at start (inclusive) and ending at end (exclusive)

    BitVector::to_bools

    fn BitVector::to_bools(self : BitVector) -> FixedArray[Bool]

    Converts the bit vector to an array of booleans

    BitVector::to_string

    fn BitVector::to_string(self : BitVector) -> String

    Converts the bit vector to a string representation, 1 for set bits, 0 for clear bits

    BitVector::xor

    fn BitVector::xor(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise XOR operation on bit vectors, returns a new bit vector

    and

    fn and(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise AND operation on bit vectors, returns a new bit vector

    bit_mask

    fn bit_mask(bit_pos : Int) -> UInt64
    Creates a bit mask at the specified position (that bit is 1, others are 0)

    clear

    fn clear(self : BitVector, index : Int) -> BitVector

    Clears the bit at the specified position to 0, returns a new bit vector

    concat

    fn concat(self : BitVector, other : BitVector) -> BitVector

    Concatenates two bit vectors, returns a new bit vector

    count_ones

    fn count_ones(self : BitVector) -> Int

    Counts the number of 1s in the bit vector

    count_zeros

    fn count_zeros(self : BitVector) -> Int

    Counts the number of 0s in the bit vector

    find_first_set

    fn find_first_set(self : BitVector) -> Int

    Finds the index of the first bit set to 1 in the vector, returns -1 if none

    flip

    fn flip(self : BitVector, index : Int) -> BitVector

    Flips the value of the bit at the specified position, returns a new bit vector

    flip_all

    fn flip_all(self : BitVector) -> BitVector

    Flips all bits in the vector, returns a new bit vector

    from_bools

    fn from_bools(bools : FixedArray[Bool]) -> BitVector

    Creates a bit vector from an array of booleans

    from_ints

    fn from_ints(ints : FixedArray[UInt64], length : Int) -> BitVector

    Creates a bit vector from an array of integers

    from_string

    fn from_string(s : String) -> BitVector

    Creates a bit vector from a string ("1" for set bits, "0" for clear bits)

    get

    fn get(self : BitVector, index : Int) -> Bool

    Gets the boolean value at the specified position in the bit vector

    is_all_ones

    fn is_all_ones(self : BitVector) -> Bool

    Checks if the bit vector is all ones

    is_all_zeros

    fn is_all_zeros(self : BitVector) -> Bool

    Checks if the bit vector is all zeros

    length

    fn length(self : BitVector) -> Int

    Gets the length of the bit vector

    low_bits_mask

    fn low_bits_mask(pos : Int) -> UInt64
    Creates a mask from 0 to the specified position (those bits are 1)

    lowest_set_bit

    fn lowest_set_bit(x : UInt64) -> Int

    Helper function: Find the position of the lowest 1 bit in an integer

    lowest_unset_bit

    fn lowest_unset_bit(x : UInt64) -> Int

    Finds the index of the first bit set to 0 in the integer, returns -1 if none

    new

    fn new(length : Int) -> BitVector

    Creates a bit vector of specified length with all bits set to 0

    not

    fn not(self : BitVector) -> BitVector

    ones

    fn ones(length : Int) -> BitVector

    Creates a bit vector of specified length with all bits set to 1
    fn or(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise OR operation on bit vectors, returns a new bit vector

    set

    fn set(self : BitVector, index : Int) -> BitVector

    Sets the bit at the specified position to 1, returns a new bit vector

    slice

    fn slice(self : BitVector, start : Int, end : Int) -> BitVector

    Extracts a sub-vector from the bit vector, starting at start (inclusive) and ending at end (exclusive)

    to_bools

    fn to_bools(self : BitVector) -> FixedArray[Bool]

    Converts the bit vector to an array of booleans

    to_string

    fn to_string(self : BitVector) -> String

    Converts the bit vector to a string representation, 1 for set bits, 0 for clear bits

    uint64_max

    fn uint64_max() -> UInt64

    xor

    fn xor(self : BitVector, other : BitVector) -> BitVector

    Performs bitwise XOR operation on bit vectors, returns a new bit vector

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    Š 2026 mooncakes.io