zimurgh / bits-bytestring

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

shiftR is incorrect for multiples of 8 #6

Open crockeea opened 5 years ago

crockeea commented 5 years ago
shiftR bs i
      | i `mod` 8 == 0 =
        B.take (B.length bs) $ B.append
          (B.replicate (i `div` 8) 0)
          (B.drop (i `div` 8) bs)

should be

shiftR bs i
      | i `mod` 8 == 0 =
        B.take (B.length bs) $ B.append
          (B.replicate (i `div` 8) 0)
          (B.take(i `div` 8) bs)
webhive commented 1 year ago

With original code

λ> B.unpack $ (B.pack [0xFF, 0, 0, 0]) `shiftR` 8
[0,0,0,0]

After your update buffer becomes shorter

λ> B.unpack $ (B.pack [0xFF, 0, 0, 0]) `shiftR` 8
[0,255]
webhive commented 1 year ago

So finally it should become something like this

  shiftR bs i
      | i `mod` 8 == 0 =
        B.take (B.length bs) $ B.replicate (i `div` 8) 0 <> B.take (i `div` 8) bs <> B.drop (i `div` 8) bs
     ...