bitwise & doesn’t work with bytes in kotlin

Kolin provides bitwise operator-like infix functions available for Int and Long only. So it’s necessary to convert bytes to ints to perform bitwise ops: val b : Byte = 127 val res = (b.toInt() and 0x0f).toByte() // evaluates to 15 UPDATE: Since Kotlin 1.1 these operations are available directly on Byte. From bitwiseOperations.kt: @SinceKotlin(“1.1”) public … Read more

The difference between logical shift right, arithmetic shift right, and rotate right

First remember that machine words are of fixed size. Say 4, and that your input is: +—+—+—+—+ | a | b | c | d | +—+—+—+—+ Then pushing everything one position to the left gives: +—+—+—+—+ | b | c | d | X | +—+—+—+—+ Question what to put as X? with a … Read more

What does a bitwise shift (left or right) do and what is it used for?

Here is an applet where you can exercise some bit-operations, including shifting. You have a collection of bits, and you move some of them beyond their bounds: 1111 1110 << 2 1111 1000 It is filled from the right with fresh zeros. 🙂 0001 1111 >> 3 0000 0011 Filled from the left. A special … Read more

Have you ever had to use bit shifting in real projects? [closed]

I still write code for systems that do not have floating point support in hardware. In these systems you need bit-shifting for nearly all your arithmetic. Also you need shifts to generate hashes. Polynomial arithmetic (CRC, Reed-Solomon Codes are the mainstream applications) or uses shifts as well. However, shifts are just used because they are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)