What is Kotlin equivalent for bitwise or with assignment '|='?
Asked Answered
B

2

46

How to write in Kotlin:

flags |= newFlag

Only what I have found is:

flags = flags or newFlag

Is there a build-in bitwise or operator with assignment?

Borsch answered 13/5, 2014 at 15:5 Comment(11)
Currently, there's no such operatorHawkweed
@AndreyBreslav Are there any build-in functions for flags? Or should I define own? Btw. What was the reason to 'remove' such operator? (Since e.g. for Android development, it is quite useful.)Borsch
@TN., there's a feature request for that: youtrack.jetbrains.com/issue/KT-1440. In it's comments you can find an interesting discussion, explaining why bitwise assignments are missing, with code samples included. You can vote for this feature with "thumbs up" button, probably we'll have 'em implemented one day, who knows.Blandina
@Borsch There are no built-in bitwise functions besides standard and, or, not and xor. What functions for flags would you like to see?Hawkweed
@AndreyBreslav For instance, '|=' (addFlags), '&=' (removeFlags) that are useful while manipulating with flags. (Android is full of flags:)Borsch
@AndreyBreslav Not nice, but it could be: or=, and=. Or highlevel functions such as: addFlags (|=), removeFlags (&= ~), keepFlags (&=) -- or some better names.Borsch
@Borsch I think one can write reasonably convenient functions here, but they won't have the assignment semantics of |=/&=: a function can not mutate a variable passed to it. One possibility is to allow that for inline functions, but I only thought of this idea just now and have to evaluate it furtherHawkweed
@AndreyBreslav Ok, no ref like in C#? How would you recommend to work with flags in Kotlin? Is it possible to simplify work with flags using some advanced Kotlin features (for instance, equivalent for C# code: someObject.someMember &= ~flags).Borsch
@Borsch I'd write a simple wrapper class named Flags with operations like add(Int) and delete(Int), flip() etc. Maybe it makes sense to even add such class to the standard library.Hawkweed
@AndreyBreslav Is it possible to map this using annotations to int? (Since someObject.someMember is from Android SDK.)Borsch
@Borsch No, you can't map it to int, but you can have an explicit toInt() function on itHawkweed
A
30

There is no built in bitwise-or assignment operator in Kotlin (yet).

Arietta answered 13/5, 2014 at 18:12 Comment(5)
Not available in 2020?Balliett
👋 It's been a long time since I checked up on Kotlin, but according to the grammar, no, still not available in 2020. Also, since bitwise operations are now on the keywords or and and, I would no longer expect them to be added to KotlinArietta
It's 2021 now.. please vote and hopefully sooner or later Kotlin will catch up the rest of the languages on thisCloudlet
It's 2022 and still nothing in this area… ;-( And ternary operator replaced with if-else hurts my eyes…Agate
it's being considered as part of the K2 compiler designBarby
T
0

As you wrote yourself this would do the job:

var flag = false
for (i in 1..5) {
    flag = flag or someCondition(i, flag)
}

fun someCondition(i: Int, flag: Boolean): Boolean {
    println("i: $i, flag: $flag")
    return true
}

Outputs:

i: 1, flag: false
i: 2, flag: true
i: 3, flag: true
i: 4, flag: true
i: 5, flag: true

This is because or does a logical or operation (both expressions are evaluated). Therefore it would also keep flag to true even if someCondition() would return false at a later iteration.

If its just about making sure a true boolean stays true then this code does the job as well. If you're coming from Java this style might be more easy to understand:

var flag = false
for (i in 1..5) {
    flag = someCondition(i, flag) || flag
}
i: 1, flag: false
i: 2, flag: true
i: 3, flag: true
i: 4, flag: true
i: 5, flag: true

However be advised it will lead to unwanted behaviour due to short-circuit evaluation (||) when chaining more conditions:

var flag = false
for (i in 1..5) {
    flag = someCondition(i, flag) || someOtherCondition() || flag
}

fun someCondition(i: Int, flag: Boolean): Boolean {
    println("i: $i, flag: $flag")
    return true
}

fun someOtherCondition(): Boolean {
    println("Nobody calls me :(")
    return true
}

Leads to:

i: 1, flag: false
i: 2, flag: true
i: 3, flag: true
i: 4, flag: true
i: 5, flag: true

So it's a good idea to adapt to or anyways quickly.

Toulouselautrec answered 27/3, 2024 at 17:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.