Disjunction Operation in Android: 2-minutes learning

--

Disjunction operation is a part of bitwise operations that we have in Java and Kotlin. A bitwise operation corresponds to a logical operation involving binary numbers. The two simplest logical operations are AND and OR.

    0101 (decimal 5)
AND 0011 (decimal 3)
= 0001 (decimal 1)

A bitwise AND is a binary operation that takes two equal-length binary representations. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1; otherwise, the result is 0.

   0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)

A bitwise OR is a binary operation that takes two-bit patterns of equal length. The result in each position is 0 if both bits are 0; otherwise, the result is 1.

Thus, in summary with AND we have a multiplication (xy), and with OR we have a sum (x + y).

But where do we have a disjunction operation in Android? 🤷‍♂️

XML layout attributes

In XML layout you have already seen or used the pipe signal ( | ) in an attribute. For example, below we have three combinations of values for the InputType in an EditText.

android:inputType="textCapSentences|textAutoCorrect"
android:inputType="text|textAutoComplete"
android:inputType="textCapSentences|numberPassword"

What does the pipe signal mean? This is a disjunction operation between two integer values! 💡

Now we can think about doing this programmatically, and here we have a major difference between Java and Kotlin. Examples:

Java

//AND bitwise operation
System.out.println(10 & 12)
//OR bitwise operation
System.out.println(10 | 12)

Kotlin

//AND bitwise operation
println(10 and 12)
//OR bitwise operation
println(10 or 12)

To carry out the first InputType example programmatically in Kotlin:

val inputType = 
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or InputType.TYPE_TEXT_FLAG_AUTO_CORRECT

Therefore, this is a little tip that can help in cases where a programmatic approach is needed using some XML Android attribute for some layout implementation.

--

--