aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/BitManipulation.kt
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-06-14 21:28:31 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-06-14 21:28:31 +0100
commit22f3463e3128947df69cb6444c82bd73350d5cc5 (patch)
treefff1be79d064c2605189c18b3b3543ec624aa429 /src/main/kotlin/BitManipulation.kt
parentcf0f98348c5f4df57d3adb44418cc2911e7b37b7 (diff)
downloadKGB-22f3463e3128947df69cb6444c82bd73350d5cc5.tar.xz
KGB-22f3463e3128947df69cb6444c82bd73350d5cc5.zip
Move bit manipulation helpers out into their own object
Diffstat (limited to 'src/main/kotlin/BitManipulation.kt')
-rw-r--r--src/main/kotlin/BitManipulation.kt27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main/kotlin/BitManipulation.kt b/src/main/kotlin/BitManipulation.kt
new file mode 100644
index 0000000..a9f9493
--- /dev/null
+++ b/src/main/kotlin/BitManipulation.kt
@@ -0,0 +1,27 @@
+object BitManipulation {
+
+ fun bytesToWord(msb: Int, lsb: Int): Int {
+ return msb.shl(8) + lsb
+ }
+
+ fun getMsb(value: Int): Int {
+ return value.shr(8)
+ }
+
+ fun getLsb(value: Int): Int {
+ return value.and(0xFF)
+ }
+
+ fun validateUnsigned8Bit(value: Int) {
+ if(value < 0 || value > 255) {
+ throw IllegalArgumentException("Value $value is not an unsigned 8-Bit value")
+ }
+ }
+
+ fun validateUnsigned16Bit(value: Int) {
+ if(value < 0 || value > 65535) {
+ throw IllegalArgumentException("Value $value is not an unsigned 16-Bit value")
+ }
+ }
+
+} \ No newline at end of file