1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.permission.access.util
18 
hasAnyBitnull19 fun Int.hasAnyBit(bits: Int): Boolean = this and bits != 0
20 
21 fun Int.hasBits(bits: Int): Boolean = this and bits == bits
22 
23 infix fun Int.andInv(other: Int): Int = this and other.inv()
24 
25 inline fun Int.flagsToString(flagToString: (Int) -> String): String {
26     var flags = this
27     return buildString {
28         append("[")
29         while (flags != 0) {
30             val flag = 1 shl flags.countTrailingZeroBits()
31             flags = flags andInv flag
32             append(flagToString(flag))
33             if (flags != 0) {
34                 append('|')
35             }
36         }
37         append("]")
38     }
39 }
40