Simple Application of enumeration, bitwise AND or operation, and displacement, and enumeration operation
When writing a program, we often encounter various settings, for example, set the dock status of the four sides of the window (for example, whether the top side is docked on the top of the parent control, whether the left side is docked on the left side of the parent control, and whether the right side is docked on the right side of the parent Control) whether the top and bottom sides are docked on the bottom side of the parent control.
Solution 1
Perhaps the first answer you think of is to set four BOOL values. The Code implemented in C language may be like this.
# Include <stdio. h> typedef char BOOL; # define True 1 # define False 0int main (int argc, const char * argv []) {// define BOOL isTop; BOOL isLeft; BOOL isRight; BOOL isBottom; // value isTop = True; isLeft = False; isRight = True; isBottom = False; // use printf ("% d \ n", isTop ); printf ("% d \ n", isLeft); printf ("% d \ n", isRight); printf ("% d \ n", isBottom); return 0 ;}
This is the most common method, but this is only the most stupid one, because you need 4 values to implement the entire logic. It is the most stupid and commonly used, but it is also the simplest and easiest to understand, but you should write it four times when assigning values.
Solution 2
If you are familiar with the storage format and bitwise AND or operations of data in the memory, you may think of this to solve this problem. It is easier for you to assign values like this.
# Include <stdio. h> # define isDockTop (dock) isDock (dock, isTop) # define isDockLeft (dock) isDock (dock, isLeft) # define isDockRight (dock) isDock (dock, isRight) # define isDockBottom (dock) isDock (dock, isBottom) typedef char BOOL; typedef enum {isTop = 1, isLeft = 2, isRight = 4, isBottom = 8} Dock; # define True 1 # define False 0 BOOL isDock (Dock dock, Dock dockTo) {return (dock & dockTo)> 0;} int main (int argc, const char * argv []) {// define the char dock; // assign a value to the dock = isTop | isRight; // use printf ("isTop \ t % d \ n ", isDockTop (dock); printf ("isLeft \ t % d \ n", isDockLeft (dock); printf ("isRight \ t % d \ n ", isDockRight (dock); printf ("isBottom \ t % d \ n", isDockBottom (dock); return 0 ;}
We may be familiar with this encoding style, because when assigning values, we may often see this bitwise and operation assignment, because this assignment is too convenient. Why is that enumeration value written? I will not go into details here because of the memory data stored in the C language. This is too basic.
Maybe you think It's okay now, but the enumerated value is too hard to write. Perhaps we can use the following method to make enumeration easier.
typedef enum{ isTop = 0B0001, isLeft = 0B0010, isRight = 0B0100, isBottom = 0B1000} Dock;
Directly write binary numbers into the file. Char only has eight bits, but 32 bits such as int have long been dazzled. Perhaps we can quickly and conveniently solve this problem through displacement.
Solution 2 Final Version
# Include <stdio. h> # define isDockTop (dock) isDock (dock, isTop) # define isDockLeft (dock) isDock (dock, isLeft) # define isDockRight (dock) isDock (dock, isRight) # define isDockBottom (dock) isDock (dock, isBottom) typedef char BOOL; typedef enum {isTop = 1, isLeft = 1 <1, isRight = 1 <2, isBottom = 1 <3} Dock; # define True 1 # define False 0 BOOL isDock (Dock dock, Dock dockTo) {return (dock & dockTo)> 0 ;} int main (int argc, const char * argv []) {// define char dock; // assign a value to dock = isTop | isRight; // use printf ("isTop \ t % d \ n", isDockTop (dock); printf ("isLeft \ t % d \ n", isDockLeft (dock )); printf ("isRight \ t % d \ n", isDockRight (dock); printf ("isBottom \ t % d \ n", isDockBottom (dock); return 0 ;}
Understanding of bitwise OR operations in Mark Enumeration
There is only one result of your bitwise OR, but two bits are set.
This toString is the default toString called, so it will be displayed in G format.
Because you have set the [Flags] flag, the results are combined Based on Multiple Digits to display the list of these constant names separated by delimiters.
Description of the G format in MSDN:
========================================================== ====================
If value is equal to a named enumerated constant, the name of the constant is returned; otherwise, the equivalent decimal number of value is returned.
For example, assume that the unique enumerated constant is named "Red" and its value is 1. If the value is set to 1, this format returns "Red ". However, if the value is set to 2, this format returns "2 ".
-Or-
If the FlagsAttribute custom attribute is applied to enumeration, value is considered as a bit field, which contains one or more flags consisting of one or more bits.
If value is equal to a combination of named enumerated constants, a list of these constant names separated by delimiters is returned. Search for a flag in value, from a flag with the maximum value to a flag with the minimum value. For each sign corresponding to the bit field in value, the constant name is connected to the list separated by separators. The value of the tag is not considered, and the next tag is searched.
If value is not equal to the combination of named enumerated constants, the equivalent decimal number of value is returned.
========================================================== ======================
For the displacement operation in JAVA>
Char ranges from 0 ~ 65535. In binary notation, It is 0000 0000 0000 0000 to 1111 1111 1111 1111
The byte range is-128 ~ 127. In binary notation, It is 1000 0000 to 0111 1111.
You can see that a char can be saved in two bytes.
For example, suppose cData = 1101 0101 0001 0001:
> 8 means that 8 bits are shifted to the right, 8 bits are shifted to the right, and 8 bits are shifted to the right. Because the first part is 1, we add 1 in the first part to get 1111 1111 1101 0101. however, at this time, we want to get 1101 0101 instead of the first eight 1 s, so we use 0xff & for processing. 0xff is 0000 0000 1111 1111. & that is, when both operands are 1, the bit is 1, that is, the last 8 bits are obtained. So 0000 0000 1111 1111 & 1111 1111 1101 0101 = 0000 0000 1101 0101. In this way, the first 8 bits of cData are obtained. The bData is stored in [0].
> 0: No operation. It just says it looks nice. 0xff & cData directly extracts the last 8 bits. In this example, 0000 0000 1111 1111 & 1101 0101 0001 0001 = 0000 0000 0001 0001. in this way, the last 8bit of cData is obtained. store bData [1].