Is this the data construction 'or' Enum type in Kotlin?

HelloCW :

The Code A is from https://github.com/android/camera/blob/master/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/utils/ViewExtensions.kt

Is this the data construction 'or' Enum type ? is FLAGS_FULLSCREEN assigned a value by system configuration? sometimes it's View.SYSTEM_UI_FLAG_FULLSCREEN , sometimes it's View.SYSTEM_UI_FLAG_LAYOUT_STABLE, right?

Is the keyword 'or' defined by Kotlin?

Code A

const val FLAGS_FULLSCREEN =
        View.SYSTEM_UI_FLAG_LOW_PROFILE or
                View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
Todd :

The or syntax in Kotlin is used to do a bitwise OR. Each one of the constants (View.SYSTEM_UI_FLAG_LOW_PROFILE, View.SYSTEM_UI_FLAG_FULLSCREEN, etc...) represents an integer value that is a power of two. By or-ing them together we create a single integer value.

For example, suppose we have three constants A (value 1), B (value 2), and C (value 4). If we wanted to create a value that is the combination of A and C, we would or them together.

This is their binary and integer representations:

A = 001 (1)
C = 100 (4)
    ---
    101

For the sake of completeness, B would be `010`.

And by looking at 0101, we can tell (by using bitwise AND) which flags were set (A, and C) and which were not (B).

So yes, we could view it as a kind of enumeration, but not a java/kotlin enum.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=132990&siteId=1