openharmony 鸿蒙 arkts-apis-arkts-collections-BitVector

2026-08-25 浏览 (1)

Class (BitVector)

A linear data structure that is implemented on arrays. A bit vector stores bit values and provides bit-level storage and processing.

NOTE

  • The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • This module can be imported only to ArkTS files (with the file name extension .ets).

Decorator: @Sendable

Modules to Import

import { collections } from '@kit.ArkTS';

Properties

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

NameTypeRead OnlyOptionalDescription
lengthnumberYesNoNumber of elements in a bit vector.

constructor

constructor(length: number)

Constructor used to create a bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
lengthnumberYesLength of the bit vector.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);

push

push(element:number): boolean

Adds an element at the end of this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesElement to add. The value 0 indicates bit value 0, and other values indicate bit value 1.

Return value

TypeDescription
booleanOperation result. The value true is returned if the element is added; otherwise, false is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200011The push method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]

pop

pop(): number

Removes the last element from this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
numberElement (bit value) removed.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The pop method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let res = bitVector.pop(); // bitVector: [0, 1, 0, 1]
console.info("bitVector pop:", res); // 0

has

has(element: number, fromIndex: number, toIndex: number): boolean

Checks whether a bit value is included in a given range of this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value. The value 0 indicates bit value 0, and other values indicate bit value 1.
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (inclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Return value

TypeDescription
booleanCheck result. The value true is returned if the bit value exists; otherwise, false is returned.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200001The value of fromIndex or toIndex is out of range.
10200011The has method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let res0: boolean = bitVector.has(0, 1, 4);
console.info("bitVector has 0:", res0); // true

setBitsByRange

setBitsByRange(element: number, fromIndex: number, toIndex: number): void

Sets elements in a given range in this bit vector to a bit value.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value to set. The value 0 indicates bit value 0, and other values indicate bit value 1.
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The setBitsByRange method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
bitVector.setBitsByRange(1, 1, 3); // bitVector: [0, 1, 1, 1, 0]

setAllBits

setAllBits(element: number): void

Sets all elements in this bit vector to a bit value.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value to set. The value 0 indicates bit value 0, and other values indicate bit value 1.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200011The setAllBits method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
bitVector.setAllBits(1); // bitVector: [1, 1, 1, 1, 1]

getBitsByRange

getBitsByRange(fromIndex: number, toIndex: number): BitVector

Obtains bit values within a given range of this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Return value

TypeDescription
BitVectorBit vector containing the bit values obtained.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The getBitsByRange method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let bitVector2 = bitVector.getBitsByRange(1, 3); // bitVector2: [1, 0]
console.info("bitVector2 length:", bitVector2.length); // 2

resize

resize(size: number): void

Resizes this bit vector.

If size is greater than the length of the existing bit vector, the bit vector is extended, and elements of the extra part are set to 0.

If size is less than or equal to the length of the existing bit vector, the bit vector is shrunk according to the size.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
sizenumberYesNew length.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200011The resize method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
bitVector.resize(10); // bitVector: [0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
console.info("bitVector get bit vector's length:", bitVector.length); // 10
bitVector.resize(3); // bitVector: [0, 1, 0]
console.info("bitVector get bit vector's length:", bitVector.length); // 3

getBitCountByRange

getBitCountByRange(element: number, fromIndex: number, toIndex: number): number

Counts the number of bit values in a given range of this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value. The value 0 indicates bit value 0, and other values indicate bit value 1.
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Return value

TypeDescription
numberNumber of bit values.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The getBitCountByRange method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let res: number = bitVector.getBitCountByRange(1, 1, 4);
console.info("bitVector getBitCountByRange:", res); // 2

getIndexOf

getIndexOf(element: number, fromIndex: number, toIndex: number): number

Returns the index of the first occurrence of a bit value in this bit vector. If the bit value is not found, -1 is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value. The value 0 indicates bit value 0, and other values indicate bit value 1.
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Return value

TypeDescription
numberIndex of the first occurrence of the bit value. If the bit value is not found, -1 is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The getIndexOf method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let res: number = bitVector.getIndexOf(0, 1, 4);
console.info("bitVector getIndexOf:", res); // 2

getLastIndexOf

getLastIndexOf(element: number, fromIndex: number, toIndex: number): number

Returns the index of the last occurrence of a bit value in this bit vector. If the bit value is not found, -1 is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementnumberYesBit value. The value 0 indicates bit value 0, and other values indicate bit value 1.
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Return value

TypeDescription
numberIndex of the last occurrence of the bit value. If the bit value is not found, -1 is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The getLastIndexOf method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let res: number = bitVector.getLastIndexOf(0, 1, 4);
console.info("bitVector getLastIndexOf:", res); // 2

flipBitByIndex

flipBitByIndex(index: number): void

Flips the bit value (from 0 to 1 or from 1 to 0) at a given index in this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex. If index is less than 0 or greater than or equal to length, an error is reported.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of index is out of range.
10200011The flipBitByIndex method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
bitVector.flipBitByIndex(3); // bitVector: [0, 1, 0, 0, 0]

flipBitsByRange

flipBitsByRange(fromIndex: number, toIndex: number): void

Flips the bit values (from 0 to 1 or from 1 to 0) in a given range in this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
fromIndexnumberYesStart index of the range (inclusive). If fromIndex is less than 0 or greater than or equal to toIndex, an error is thrown.
toIndexnumberYesEnd index of the range (exclusive). If toIndex is less than 0 or greater than or equal to length, an error is thrown.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.
10200001The value of fromIndex or toIndex is out of range.
10200011The flipBitsByRange method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
bitVector.flipBitsByRange(1, 4); // bitVector: [0, 0, 1, 0, 0]

values

values(): IterableIterator<number>

Returns an iterator object that contains the value of each element in this bit vector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<number>Bit vector iterator object.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The values method cannot be bound.
10200201Concurrent modification error.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
let iter: IterableIterator<number> = bitVector.values();
let temp: IteratorResult<number> = iter.next();
while (!temp.done) {
  console.info(JSON.stringify(temp.value));
  temp = iter.next();
} // 0, 1, 0, 1, and 0 are returned in sequence.

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<number>

Returns an iterator, each item of which is a JavaScript object.

NOTE

This API cannot be used in .ets files.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<number>Iterator obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The Symbol.iterator method cannot be bound.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);

for (let item of bitVector) {
  console.info("value: " + item);
}

[index: number]

[index: number]: number

Returns the element at a given index in this BitVector.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the element. The index starts from zero.

Return value

TypeDescription
numberNumber data type.

Example

let bitVector: collections.BitVector = new collections.BitVector(0);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0);
bitVector.push(1);
bitVector.push(0); // bitVector: [0, 1, 0, 1, 0]
console.info("BitVector Element Index at 1: " + bitVector[1]);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-arkts-collections-Float32Array

openharmony 鸿蒙 arkts-apis-arkts-collections-ArrayBuffer

openharmony 鸿蒙 js-apis-lightweightset

openharmony 鸿蒙 errorcode-source-obfuscation

openharmony 鸿蒙 arkts-apis-arkts-utils-ASON

openharmony 鸿蒙 arkts-apis-arkts-collections-Uint8ClampedArray

openharmony 鸿蒙 errorcode-tsc

openharmony 鸿蒙 js-apis-deque

openharmony 鸿蒙 js-apis-worker

openharmony 鸿蒙 js-apis-util

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7zbuCeLz