Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
[#1] Add left and right shift operations for byte array
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
rlespinasse committed Oct 11, 2017
1 parent 01b43a0 commit 5110523
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 23 deletions.
6 changes: 6 additions & 0 deletions bit_methods.go → bit_operations.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gobits

const (
byteLength = 8
lsb = 0
msb = byteLength - 1
)

// ContainsBit test if a bit is contains in the data
func ContainsBit(data byte, bit uint8) bool {
return ContainsBits(data, SetBit(0x00, bit))
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions bits_methods.go

This file was deleted.

21 changes: 21 additions & 0 deletions bits_operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gobits

// ContainsBits test if some bits is contains in a data
func ContainsBits(data, bits byte) bool {
return data&bits == bits
}

// SetBits affect the bits to a data
func SetBits(data, bits byte) byte {
return data | bits
}

// UnsetBits unaffect the bits to a data
func UnsetBits(data, bits byte) byte {
return data &^ bits
}

// ExtractBits get a byte as subset of another byte
func ExtractBits(data byte, lsbPosition, msbPosition uint8) byte {
return data << (msb - msbPosition) >> (msb + lsbPosition - msbPosition)
}
2 changes: 1 addition & 1 deletion bits_methods_test.go → bits_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestUnsetBits(t *testing.T) {
}

func TestExtractBits(t *testing.T) {
var result = ExtractBits(0xDA, 4, 3)
var result = ExtractBits(0xDA, 3, 4)
if result != 0x03 {
t.Errorf("ExtractBits don't work: %08b", result)
}
Expand Down
27 changes: 27 additions & 0 deletions bytearray_operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gobits

// RightShift apply right shift operation to byte array data
func RightShift(data []byte, shift uint8) []byte {
var dataLength = len(data)
result := make([]byte, dataLength)
for i := dataLength - 1; i >= 0; i-- {
if i > 0 {
result[i-1] = data[i] >> (byteLength - shift)
}
result[i] = result[i] | (data[i] << shift)
}
return result
}

// LeftShift apply left shift operation to byte array data
func LeftShift(data []byte, shift uint8) []byte {
var dataLength = len(data)
result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
if i < dataLength-1 {
result[i+1] = data[i] << (byteLength - shift)
}
result[i] = result[i] | (data[i] >> shift)
}
return result
}
20 changes: 20 additions & 0 deletions bytearray_operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gobits

import (
"reflect"
"testing"
)

func TestRightShift(t *testing.T) {
var result = RightShift([]byte{0x99, 0xBA}, 1)
if !reflect.DeepEqual(result, []byte{0x33, 0x74}) {
t.Errorf("RightShift don't work: %x", result)
}
}

func TestLeftShift(t *testing.T) {
var result = LeftShift([]byte{0x99, 0xBA}, 1)
if !reflect.DeepEqual(result, []byte{0x4C, 0xDD}) {
t.Errorf("LeftShift don't work: %x", result)
}
}

0 comments on commit 5110523

Please sign in to comment.