Check if a flag is included — bitwise helps Android dev to check a flag is enabled

Toan Tran
1 min readApr 13, 2018

--

For Android developer, it is normal to see something like

You have several flags noticeable here like: isGame or largeHeap

Somewhere in your code, a common library may want to know if your app is largeHeap enabled so that it would be able to use more memory to boost performance of some specific operation.

How to check it in an elegant way?

fun isUsingLargeHeap(context: Context): Boolean {
val applicationInfo = context.applicationInfo
return applicationInfo.flags and ApplicationInfo.FLAG_LARGE_HEAP == ApplicationInfo.FLAG_LARGE_HEAP
}

This snippet of code is using “and” bitwise operator in Kotlin to handle that case

--

--

Toan Tran
Toan Tran

Written by Toan Tran

Learning to build my own business

Responses (1)