Java: | (or operation) and multi-choice judgment

Source: Internet
Author: User

Today we need to make a multi-choice judgment in the program, and suddenly think of the x |= y that I used to encounter before | Z In this way, I also understand that this is the intention of multi-choice, but why can achieve the multi-select operation we want, I really did not study.

This morning, Baidu a bit, the search for two articles:

1) This is written in C, the truth is the same. Excerpt from: http://blog.csdn.net/arduousbonze/article/details/1619096

The nature of the three
XOR operation: Same as 0, different 1
and operation: 1 is 1, 0 is 0
Or operation: 1 is 1

Define the following macro: (observe its characteristics)

#define FLAG_DUPLICATE 0x01//Expand binary 00000001
#define FLAG_VISIBLE 0x02//Expand binary 00000010
#define FLAG_CANSELECT 0x04//Expand binary 00000100
#define FLAG_LABELED 0x08//Expand binary 00001000
#define FLAG_BACKGROUND 0x10//Expand binary 00100000
#define Flag_showoutline 0x20//Expand binary 01000000
#define FLAG_SHOWATEAGLE 0x40//Expand binary 10000000

(Note: Octal begins with 0, such as 0123 (the difference between decimal 123) and hexadecimal starts with 0x, such as 0x40)
The purpose of setting the macro is to be able to use the nature of the bitwise operation to determine the state of the check box.

Suppose you use int nstatus; to store the result of a check box, you can do this:

1. If a check box is selected: nstatus=nstatus| Flag_duplicate; (Other similar)

2. What if a check box is canceled? Should do this:
int mask=0xffffffff^flag_duplicate;//(The position of "1" in Flag_duplicate is 0, the other bits are 1)
nstatus=nstatus&mask; (Other similar)

3. How can I tell if a check box is selected? Do this:
if (nstatus&flag_duplicate==flag_duplicate)
cout<< "SELECT" <<endl;
Else
cout<< "Unchecked" <<endl;

Example:

Cancel check box
void __fastcall layerinfo::setvisible (bool bvisible)
{
if (bvisible)
m_nstatus=m_nstatus| flag_visible;
Else
{
int mask = 0xffffffff^flag_visible;
m_nstatus=m_nstatus&mask;
}
Layer->visible= bvisible;
}

Determines whether a check box is selected
if (m_nstatus&flag_showoutline==flag_showoutline)
layer->symbol->outline=true;
Else
layer->symbol->outline=false; Another article: http://blog.csdn.net/battlehawk/article/details/5271327 I wrote a simple test | ( or operations), according to the output of the results, see the law came to it:

System.out.println (-1 |-1); Output-1
System.out.println (-1 | 0); Output-1
System.out.println (-1 | 1); Output-1

System.out.println (0 | 0); Output 0
System.out.println (0 | 1); Output 1
System.out.println (0 | 2); Output 2

2011-12-31

Java: | (or operation) and multi-choice judgment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.