Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Java's bitwise operators in Kotlin?

Java has binary-or | and binary-and & operators:

int a = 5 | 10; int b = 5 & 10; 

They do not seem to work in Kotlin:

val a = 5 | 10; val b = 5 & 10; 

How do I use Java's bitwise operators in Kotlin?

like image 286
Water Magical Avatar asked Jan 27 '18 09:01

Water Magical


People also ask

What does ?: Mean in Kotlin?

In certain computer programming languages, the Elvis operator ?: is a binary operator that returns its first operand if that operand is true , and otherwise evaluates and returns its second operand.

What is SHL in Kotlin?

infix fun shl(bitCount: Int): Int. Shifts this value left by the bitCount number of bits. Note that only the five lowest-order bits of the bitCount are used as the shift distance. The shift distance actually used is therefore always in the range 0.. 31 .

How to use bitwise and bit shift operators in Kotlin?

Bitwise and bit shift operators are used on only two integral types (Int and Long) to perform bit-level operations. To peform these operations, Kotlin provides 7 functions using infix notation. 1. or The or function compares corresponding bits of two values.

What is bitwise operator in Java?

Bitwise operators in Java. Bitwise operators are used to perform manipulation of individual bits of a number. They can be used with any of the integral types (char, short, int, etc). They are used when performing update and query operations of Binary indexed tree. This operator is binary operator, denoted by ‘|’.

What are the operators supported by Kotlin?

Kotlin supports the following operators and special symbols: +, -, *, /, % - mathematical operators. * is also used to pass an array to a vararg parameter. =. assignment operator. is used to specify default values for parameters. +=, -=, *=, /=, %= - augmented assignment operators. ++, -- - increment and decrement operators.

How do you use infix notation in Kotlin?

To peform these operations, Kotlin provides 7 functions using infix notation. The or function compares corresponding bits of two values. If either of the bits is 1, it gives 1. If not, it gives 0. For example, The and function compares corresponding bits of two values. If both bits are 1, it is evaluated to 1.


2 Answers

You have named functions for them.

Directly from Kotlin docs

Bitwise operations are represented by functions that can be called in infix form. They can be applied only to Int and Long.

for example:

val x = (1 shl 2) and 0x000FF000 

Here is the complete list of bitwise operations:

shl(bits) – signed shift left (Java's <<) shr(bits) – signed shift right (Java's >>) ushr(bits) – unsigned shift right (Java's >>>) and(bits) – bitwise and or(bits) – bitwise or xor(bits) – bitwise xor inv() – bitwise inversion 
like image 90
Suresh Atta Avatar answered Sep 24 '22 07:09

Suresh Atta


you can do this in Kotlin

val a = 5 or 10; val b = 5 and 10; 

here list of operations that you can use

shl(bits) – signed shift left (Java's <<) shr(bits) – signed shift right (Java's >>) ushr(bits) – unsigned shift right (Java's >>>) and(bits) – bitwise and or(bits) – bitwise or xor(bits) – bitwise xor inv() – bitwise inversion 
like image 31
Ali Faris Avatar answered Sep 20 '22 07:09

Ali Faris