- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Java's bitwise operators in Kotlin?
Kotlin provides a set of bitwise operators that we can use on integers. These operators can be applied only to Int and Long type variables. Here is the list of bitwise operations available in Kotlin −
shl(bits) – Signed Shift left
shr(bits) – Signed shift right
ushr(bits) – Unsigned shift right
and(bits) – Bitwise AND operator
or(bits) – Bitwise OR operator
xor(bits) – Bitwise XOR
inv() – Bitwise inversion
Kotlin does have functions for each of them.
Example: Bitwise Operators in Kotlin
The following example shows how you can implement the bitwise operators in Kotlin.
import java.lang.* fun main(args: Array<String>) { val value = 5 println("Input value: " + value) println("Bitwise Left: " + value.shl(2)) println("Bitwise Right: " + value.shr(2)) println("Bitwise unsigned shift right: " + value.ushr(2)) println("Bitwise AND: " + value.and(2)) println("Bitwise OR: " + value.or(2)) println("Bitwise XOR: " + value.xor(2)) }
Output
On execution, it will generate the following output −
Input value: 5 Bitwise Left: 20 Bitwise Right: 1 Bitwise unsigned shift right: 1 Bitwise AND: 0 Bitwise OR: 7 Bitwise XOR: 7
Advertisements