Skip to content

Commit

Permalink
fix ShiftRight
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalick committed Oct 16, 2021
1 parent 7f77dad commit 2ee47bf
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bitslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (s BitSlice) LenBytes() int {

//ShiftLeft returns shifted BitSlice, like << operation
func (s BitSlice) ShiftLeft(val int) BitSlice {
if val == 0 {
return s
}
if val < 0 {
return s.ShiftRight(-val)
}
Expand All @@ -131,13 +134,16 @@ func (s BitSlice) ShiftLeft(val int) BitSlice {

//ShiftRight returns shifted BitSlice, like >> operation
func (s BitSlice) ShiftRight(val int) BitSlice {
if val == 0 {
return s
}
if val < 0 {
return s.ShiftLeft(-val)
}
newSlice := s
sLen := s.Len()
newSlice.Slice = make([]bool, sLen)
ns := s.Slice[:val]
ns := s.Slice[:sLen-val]
nsLen := len(ns)
for i, bit := range ns {
newSlice.Slice[sLen-nsLen+i] = bit
Expand Down

0 comments on commit 2ee47bf

Please sign in to comment.