Use of masks in Android

Source: Internet
Author: User

A mask is a string of binary code that makes a bitwise AND operation of the target field, shielding the current input bit, and is sometimes referred to as a shield code.

This technique is often used in Android to set flag to determine the tag, which can be referred to the Windowmanager.java of the framework layer.

The general flag will be written in two ways that look more intuitive (assuming that the flag is set to a length of four bytes):

One:

Private final int flag_one = 1 << 0;
Private final int flag_two = 1 << 1;
Private final int flag_three = 1 << 2;
Private final int flag_four = 1 << 3;
Private final int flag_five = 1 << 4;
Private final int flag_six = 1 << 5;

Two:

Private final int flag_one = 0x00000001
Private final int flag_two = 0x00000002
Private final int flag_three = 0x00000004
Private final int flag_four = 0x00000008
Private final int flag_five = 0x00000010
Private final int flag_six = 0x00000020

The calculation involved:

Flag &= 0xFFFF;

Flag High 16 position zero, low 16 bit reserved

Flag &= ~flag_one;

Place the mark of the Flag_one position in the flag zero

Flag |= Flag_one;

Position the Flag_one in flag 1

Use of masks in Android

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.