zimurgh / bits-bytestring

BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

setBit changes length of bytestring #2

Open clample opened 7 years ago

clample commented 7 years ago

This behaviour might be desired, but I thought I would bring it up. When setBit is called, the returned bytestring doesn't contain any of the bytes after the index.

Prelude> import qualified Data.ByteString.Base16 as B16
Prelude B16> import Data.Bits
Prelude B16 Data.Bits> import Data.Bits.ByteString
Prelude B16 Data.Bits Data.Bits.ByteString> import qualified Data.ByteString as B
Prelude B16 Data.Bits Data.Bits.ByteString B> import qualified Data.ByteString.Char8 as Char8
Prelude B16 Data.Bits Data.Bits.ByteString B Char8> let zeroes = fst . B16.decode . Char8.pack $ "0000"
Prelude B16 Data.Bits Data.Bits.ByteString B Char8> B.length zeroes
2
Prelude B16 Data.Bits Data.Bits.ByteString B Char8> let newBS = setBit zeroes 0
Prelude B16 Data.Bits Data.Bits.ByteString B Char8> B.length newBS
1
Prelude B16 Data.Bits Data.Bits.ByteString B Char8> Char8.unpack . B16.encode $ newBS 
"01"

If you try to set the first bit on a long bytestring, the returned bytestring will only have 1 byte

clample commented 7 years ago

setBit x i is defined as x .|. bit i. When bit i has a shorter length than x, it seems that the B.zipWith used in the definition of .|. is just dropping the later bytes of x