diff --git a/bit_methods.go b/bit_operations.go similarity index 88% rename from bit_methods.go rename to bit_operations.go index 2dc42b7..1019f01 100644 --- a/bit_methods.go +++ b/bit_operations.go @@ -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)) diff --git a/bit_methods_test.go b/bit_operations_test.go similarity index 100% rename from bit_methods_test.go rename to bit_operations_test.go diff --git a/bits_methods.go b/bits_methods.go deleted file mode 100644 index 85c7a14..0000000 --- a/bits_methods.go +++ /dev/null @@ -1,22 +0,0 @@ -package gobits - -// ContainsBits test if b is contains in a -func ContainsBits(data, bits byte) bool { - return data&bits == bits -} - -// SetBits affect to a, the b bits -func SetBits(data, bits byte) byte { - return data | bits -} - -// UnsetBits unaffect to a, the b bits -func UnsetBits(data, bits byte) byte { - return data &^ bits -} - -// ExtractBits get a byte for subset of another byte -func ExtractBits(data byte, lsb, msb uint8) byte { - var unusedBits = 7 + msb - lsb - return data >> msb << unusedBits >> unusedBits -} diff --git a/bits_operations.go b/bits_operations.go new file mode 100644 index 0000000..4a3ec54 --- /dev/null +++ b/bits_operations.go @@ -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) +} diff --git a/bits_methods_test.go b/bits_operations_test.go similarity index 93% rename from bits_methods_test.go rename to bits_operations_test.go index 940d723..6327b65 100644 --- a/bits_methods_test.go +++ b/bits_operations_test.go @@ -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) } diff --git a/bytearray_operations.go b/bytearray_operations.go new file mode 100644 index 0000000..74e908e --- /dev/null +++ b/bytearray_operations.go @@ -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 +} diff --git a/bytearray_operations_test.go b/bytearray_operations_test.go new file mode 100644 index 0000000..f93b15c --- /dev/null +++ b/bytearray_operations_test.go @@ -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) + } +}