Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use hexadecimal for state management of Android source code

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "Android source code how to use hexadecimal for state management", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Android source code how to use hexadecimal for state management" article.

Preface

In Android source code, the management of "multistate" is always represented by hexadecimal numbers, similar to this format:

/ / ViewGroup.javaprotected int mGroupFlags;static final int FLAG_CLIP_CHILDREN = 0x1 * private static final int FLAG_CLIP_TO_PADDING = 0x2 * * static final int FLAG_INVALIDATE_REQUIRED = 0x4 * * private static final int FLAG_RUN_ANIMATION = 0x8 * * private final int FLAG_ANIMATION_DONE = 0x10 * * private static final int FLAG_PADDING_NOT_NULL = 0x20

So, have you ever wondered why hexadecimal is needed for multi-state management?

Simple state representation

To give a practical example, as a person, we will certainly have a lot of labels, such as handsome, cute, knowledgeable, witty, lazy and stingy.

For these labels, we can set different people's settings:

/ / define the entity class data class Person (var tag: String) / / modify the tag val person1 = Person ("handsome") / / judge the tag fun isCute (): Boolean {return person1.tag = = "cute"}

When a person has only one tag, it is very simple, just assign or judge the value directly. But what if a person has multiple tags?

It is also simple to use collection storage:

Val person2 = Person (mutableListOf ()) person2.tags.add ("handsome") person2.tags.add ("cute") person2.tags.remove ("cute") person2.tags.contains ("cute")

But after using the collection, the calculation becomes more complex, because both the remove and contains methods are implemented by traversing the collection, from the point of view of time complexity, the time complexity of deleting a tag or determining whether a tag exists is O (n).

Is there any way to make multiple tags as easy to use as a single tag?

Binary operation

Of course there is, otherwise this article would not be available. Before that, let's review some binary operations.

1. Bitwise and (&)

When the values of both corresponding bits are 1, the result is 1, otherwise it is 0.

For example: 0x1 & 0x4

0001 &

0100

=

0000

2. Bitwise or (|)

When one of the values of both corresponding bits is 1, the result is 1.

For example: 0x1 | 0x4

0001 |

0100

=

0101

3. Reverse (~)

Invert a number by bit.

For example: ~ 0x1

0001 ~

=

1110

Well, with these three operations, our state management is sufficient.

Introduce hexadecimal

Next, complete a complete example of state management.

/ / set hexadecimal values for all states / / cute, corresponding to binary 0001val TAG_CUTE = Ox1 / / handsome, corresponding to binary 0010 val TAG_HANDSOME = Ox2// erudite, corresponding to binary 0100val TAG_LEARNED = Ox4var personTag = 0 status increased

If a binary number wants to leave a trace of another binary number (the trace of the number 1), we can pass the OR operation, so that as long as there is a 1 in the second digit, the final result must be 1 in the same number.

Therefore, we can use this method to complete the function of state addition:

/ / add cute status personTag | = TAG_CUTE0000 | 0001 = 0001

After doing this, the number in the fourth digit of the personTag is 1, which is marked with the TAG_CUTE.

State removal

According to the above logic, the removal of the state actually requires changing the corresponding number of digits from 1 to 0.

Suppose the value of personTag now becomes the binary number 0111.

If you want to delete the TAG_CUTE attribute, you need to change the 1 in the fourth bit to 0. So what we can do is to reverse TAG_CUTE first, that is, to turn 0001 into 1110. And then do the and operation with personTag, so that the fourth bit will definitely become 0, while the values above the other bits will not change.

/ / personTag is a binary number 0111personTag & = ~ TAG_CUTE0001 ~ = 1110 & 0111140110

Finish removing the TAG_CUTE state.

State judgment

By the same token, the judgment of whether there is a certain state is actually to judge whether the value is 1 in a certain bit.

So we just need to do the and operation on the state, and if the result is 0, it means there is no such state, otherwise it means that there is this state.

/ / personTag is a binary number 0111 (personTag & TAG_CUTE)! = 00111 & 00010001

The result is not 0, so the personTag contains the state of TAG_CUTE.

Points to pay attention to

Careful friends may find that the hexadecimal value we just used skips the value of Ox3. Why?

In fact, it is not difficult to find that the so-called management state through hexadecimal, in fact, through the binary to manage the state, in the final analysis, through the number of bits in the binary to manage the state.

So when we assign a value to the state, we need to choose a binary value that occupies a single bit, such as 0001, 0010, 0100, 10, 000, and so on.

What happens if you use other values? For example, add TAG to Ox3.

/ / lazy, corresponding to binary 0011val TAG_LAZY = Ox3// to add cute status personTag | = TAG_CUTE// to increase handsome status personTag | = TAG_HANDSOME

After we added cute and handsome status, the binary value of personTag is 0011.

At this point, it is judged whether it contains laziness:

/ / whether there is laziness (personTag & TAG_LAZY)! = 00011 & 0011 = 0011

If the result is not zero, do we increase our laziness? Obviously not, I am not lazy but say I am lazy, this is a false accusation!

So do you understand the range of status values?

Why hexadecimal?

At this point, the function of managing the state through hexadecimal has been realized, it is obvious that it is much easier to manage the state in this way, and its basic principle is to complete the state management through binary calculation.

Some people want to ask again, since the essence is to complete management through binary, it is also possible to use decimal to express it, such as the example above:

/ / set the decimal value corresponding to all states / / cute, corresponding to binary 0001val TAG_CUTE = 1 / / handsome, corresponding to binary 0010 val TAG_HANDSOME = 2 val TAG_HANDSOME / erudite, corresponding to binary 0100val TAG_LEARNED = 4var personTag = 0

Isn't this the same as hexadecimal?

Basically, it's the same, but hexadecimal has the benefits of hexadecimal, which involves why hexadecimal was designed.

In a computer, there are eight bits in a byte, with a maximum value of 1111 1111. The corresponding decimal number is 255 and the corresponding hexadecimal number is FF.

So half a byte in hexadecimal can be represented by a letter, and converting to decimal is an irregular number.

For convenience, hexadecimal is generally used to represent binary in code because it can be converted to binary for a more convenient and intuitive conversion.

The above is about the content of this article on "how to use hexadecimal Android source code for state management". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report