0

I am new to android and learning java. In recent guide, i came across the method to toggle password field to normal text field.

Can someone please explain use of | in this statement ?

final EditText input = (EditText) findViewById(R.id.etCommands);

if(passTog.isChecked())
{
   input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}else {
   input.setInputType(InputType.TYPE_CLASS_TEXT);
}

Any help regarding this will be much appreciated. Thanks in advance.

Edit:

I need to know how this Bitwise OR is working infact here ? Here is complete code to avoid ambiguity regarding the variables:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.text);

    Button checkCommand = (Button)findViewById(R.id.bResults);
    final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
    final EditText input = (EditText) findViewById(R.id.etCommands);
    TextView display = (TextView) findViewById(R.id.tvResults);

    passTog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if(passTog.isChecked()){
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }else {
                input.setInputType(InputType.TYPE_CLASS_TEXT);
            }

        }
    });

}
Shumail
  • 3,103
  • 4
  • 28
  • 35
  • 4
    [Bitwise OR](http://en.wikipedia.org/wiki/Bitwise_operation#OR) – Rohit Jain Jul 18 '13 at 20:42
  • Why was he majorly downvoted? – John Strickler Jul 18 '13 at 20:43
  • 1
    I'm sure somewhere I saw a manual... – Adriano Repetti Jul 18 '13 at 20:43
  • I am new to Java - Dont Downvote please – Shumail Jul 18 '13 at 20:44
  • 2
    @JohnStrickler - Because... Why on earth would you ask this question without googling first? (regardless of your level of experience) For the record, I did **not** downvote. Though I'm tempted to... My gut reaction would be to google ["_java operators_")[http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) – jahroy Jul 18 '13 at 20:57
  • @jahroy: Actually i have an idea of Bitwise OR operator and i also googled that - What i am unable to understand is it's use here ? input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); – Shumail Jul 18 '13 at 21:01
  • Like I said... I didn't downvote. But I can _easily_ understand the rationale. Not sure what's to misunderstand... `|` is an operator. In that code it _operates_ on two variables. I'd consider writing a quick test program to see exactly what it does to those two variables. If you need us to _tell_ you what/why it does, you should provide the values of the two strings. – jahroy Jul 18 '13 at 21:02
  • Alright - p.s i have edited my question and added whole class code. – Shumail Jul 18 '13 at 21:09
  • Now that I look closer, it looks like the variables are part of a class from the Android libraries. I suggest reading the documentation for `InputType`, where you will certainly find some info. – jahroy Jul 18 '13 at 21:10
  • That's part of ``InputType`` – Shumail Jul 18 '13 at 21:11
  • 1
    Now that I look at the documentation, I'm downvoting and voting to close. It's all spelled out right there. Please learn to consult the documentation when you have a question about anything programming related. The info is almost always there (if you're willing to read). – jahroy Jul 18 '13 at 21:15
  • i am into documentation.... Thanks for your support & help :) – Shumail Jul 18 '13 at 21:15
  • 4
    **From the very top of the documentation:** "_A password field with with the password visible to the user: inputType = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD_" It's literally **the first** thing mentioned on [this page](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_TEXT). – jahroy Jul 18 '13 at 21:17
  • Thankyou for Support - Much appreciated And personally i consider it my mistake not consulting the doc first. – Shumail Jul 18 '13 at 21:21
  • A request - undo downvote please – Shumail Jul 18 '13 at 21:23
  • 2
    Absoutely not. This question is the epitome of "_shows no effort_". People who search with similar questions should find the Android documentation, not this question. – jahroy Jul 18 '13 at 21:24
  • Like i said - Apologies. – Shumail Jul 18 '13 at 21:25
  • I understand and fully support and that's why confessed my mistake and requested to undo downvote - that can also help many. Trust me. p.s "Apologies" was for some reason.. Anyways..... – Shumail Jul 18 '13 at 21:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33727/discussion-between-shumail92-and-jahroy) – Shumail Jul 18 '13 at 22:50

7 Answers7

9

That code isn't using | in an if statement. It's using it in the body of an if statement which is very different. When used there, it's a bitwise-or operation. Edit: @RohitJain has provided a much better link than mine in his comment: http://en.wikipedia.org/wiki/Bitwise_operation#OR


If it were used in an if statement like so:

if (foo() | bar())

that means "don't short circuit". If foo() returned "true" it will still evaluate bar(). If you used || and foo() returns "true", it won't evaluate bar() because it knows the result of the if statement will be "true" no matter what else happens.


For your specific question, you can see the possible values of InputType here.

InputType.TYPE_CLASS_TEXT = Constant Value: 1 (0x00000001)
InputType.TYPE_TEXT_VARIATION_PASSWORD = Constant Value: 128 (0x00000080)

Using this tool, I've calculated the result of a bitwise or will be: 129. To see how to get that value, read the wikipedia article above.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • Can you please explain how ``|`` is working in that statement ? ``input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);`` – Shumail Jul 18 '13 at 20:49
  • I'd need to know the values of `InputType.TYPE_CLASS_TEXT` and `InputType.TYPE_TEXT_VARIATION_PASSWORD` to be able to do that. But you can use this link to get the general idea: http://en.wikipedia.org/wiki/Bitwise_operation#OR – Daniel Kaplan Jul 18 '13 at 20:51
  • What class is `input`? I'd need to know that to answer that. – Daniel Kaplan Jul 18 '13 at 21:01
  • ``input`` is actually a variable ``final EditText input = (EditText) findViewById(R.id.etCommands);`` – Shumail Jul 18 '13 at 21:02
  • Thanks a lot :) Now i understand it fully.... And i consider it my mistake not consulting the Doc first... – Shumail Jul 18 '13 at 21:24
1

That's bitwise OR Operator - See this for details

1
|  is a Bitwise inclusive OR
|| is a Conditional-OR

A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1; otherwise, the result is 0. For example:

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

Check this and this out.

0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • `|` is also non-short circuited conditional OR – Steve Kuo Jul 18 '13 at 20:47
  • Thanks. I have a good idea of Bitwise OR - Studied in Digital Logic Design subject. What i am unable to understand is it's use here ? ``input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);`` – Shumail Jul 18 '13 at 20:58
  • Very strange usage tbh, it looks like its setting the input type to be whatever results in bitwise combining the `TYPE_CLASS_TEXT` and `TYPE_TEXT_VARIATION_PASSWORD`. It looks like `InputType` must be an `enum` so I guess it allows you to work with more complex input types without having to include them in `InputType`? Possibly a way to evaluate types in a situation where there could be millions of potencial types that can't be included in the `enum`? – 0x6C38 Jul 18 '13 at 21:07
1

The "|" symbol is defined as bitwise inclusive OR

Source: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1. If both of the bits are 0, the result of that bit is 0; otherwise, the result is 1.

Source: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05bitiore.htm

Paul Bailey
  • 146
  • 5
1

The first thing you should always do is consult the documentation.

Here is a link to the documentation for InputType

Here is the first thing on the page:

A password field with with the password visible to the user:

    inputType = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

It's literally the third sentence on the page.

jahroy
  • 22,322
  • 9
  • 59
  • 108
0

| means bitwise OR

|| means logical OR

tallen
  • 677
  • 6
  • 17
0

The | is BITWISE OR'ing the constants InputType.TYPE_CLASS_TEXT and InputType.TYPE_TEXT_VARIATION_PASSWORD. I'm assuming that those two constants are ints used to identify internal types the EditText object cares about, given the formatting, but it could be any variable type. If you really want to know exactly what it's doing, you would need to know the data type of InputType.TYPE_CLASS_TEXT, but we can assume thanks common practices of Java code that it is sticking constants together for a comparison later.

mezzoEmrys
  • 66
  • 2