0

I'm designing a custom keyboard for Android. I used a sample SoftKeyboard project. In LatinKeyboard.java class there is a switch statement like bellow:

switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION))
{
    //Cases     
}

I'm trying to understand this switch statement. I searched switch statement documentation but I couldn't find "&" sign in switch. I also searched on other places for solution but no solution so far.

Hamid
  • 2,852
  • 1
  • 28
  • 45

2 Answers2

3

The & has nothing to do with the switch statement; it's simply the Java bitwise AND operator. It does a bitwise "and" of the current value of options with the (constant) int-valued expression (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION). (The | in that expression is the bit-wise OR operator.) The result is an int value that is then used by the switch. It's the same thing as writing:

int value = options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
switch (value)
{
    //Cases     
}

except you avoid the need for a variable.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
3

Here & is bitwise operator which performs AND operation on provided value while | is bitwise OR operator which is performing OR operation on the values.

  • & Bitwise AND
  • | Bitwise OR

As explained in the link

 int a = 60;    /* 60 = 0011 1100 */  /*Binary*/
 int b = 13;    /* 13 = 0000 1101 */
 int c = 0;

 c = a & b;     /* 12 = 0000 1100 */ 

 c = a | b;     /* 61 = 0011 1101 */
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Thank you for the swift response. Now I understood. But how I can recognize the outcome of the switch statement? – Hamid Sep 18 '14 at 17:49
  • Well that completely depends on the `options` as I think other two variables are constants. – akash Sep 18 '14 at 18:11