aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/BitManipulation.kt
blob: a9f94938b2f6649405a8a24574401f20b25d88b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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")
    }
  }

}