This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1] Add left and right shift operations for byte array
Closes #1
- Loading branch information
1 parent
01b43a0
commit 5110523
Showing
7 changed files
with
75 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |