From fa237e88d3095894faf04d0714d10355efe11fdf Mon Sep 17 00:00:00 2001 From: weiihann Date: Thu, 26 Dec 2024 19:36:13 +0800 Subject: [PATCH] Add BitSet() --- core/trie/bitarray.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/trie/bitarray.go b/core/trie/bitarray.go index d56e7cb47..0d820bf88 100644 --- a/core/trie/bitarray.go +++ b/core/trie/bitarray.go @@ -280,13 +280,22 @@ func (b *BitArray) Equal(x *BitArray) bool { } // Returns true if bit n-th is set, where n = 0 is LSB. -// The n must be <= 255. func (b *BitArray) IsBitSet(n uint8) bool { + return b.BitSet(n) == 1 +} + +// Returns the bit value at position n, where n = 0 is LSB. +// If n is out of bounds, returns 0. +func (b *BitArray) BitSet(n uint8) uint8 { if n >= b.len { - return false + return 0 } - return (b.words[n/64] & (1 << (n % 64))) != 0 + if (b.words[n/64] & (1 << (n % 64))) != 0 { + return 1 + } + + return 0 } // Serialises the BitArray into a bytes buffer in the following format: