From 22f3463e3128947df69cb6444c82bd73350d5cc5 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Thu, 14 Jun 2018 21:28:31 +0100 Subject: Move bit manipulation helpers out into their own object --- src/main/kotlin/BitManipulation.kt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/kotlin/BitManipulation.kt (limited to 'src/main/kotlin/BitManipulation.kt') 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 -- cgit v1.2.3