In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to achieve bit operation in C#, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Bit logic non-operation
Bit logic non-operation is a single purpose, there is only one Operand. Bit logic non-operation performs a bit-by-bit non-operation on the value of an Operand, that is, if a bit is equal to 0, it is converted to 1; if a bit is equal to 1, it is converted to 0.
For example, if you do a bit logic non-operation on binary 10010001, the result is equal to 01101110, which is expressed in decimal system:
A bit logic non-operation is performed on binary 01010101 and the result is equal to 10101010. In decimal terms, 85 equals 176.
2. Bit logic and operation
Bit logic and operation the two operands are bitwise and operated. The rule of sum operation: 1 and 1 equals 1 and 0 equals 0.
For example, 10010001 (binary) & 11110000 equals 10010000 (binary).
3. Bit logic or operation
Bit logic or operation performs or operates on two operands bit by bit. Or the rule of operation is: 1 or 1 1 or 0 equals 1
0 or 0 equals 0. For example, 10010001 (binary) | 11110000 (binary) equals 11110001 (binary).
4. Bit logic XOR operation
The bit logic XOR operation performs XOR operations on two operands bitwise. The rule of XOR operation is: 1 XOR 1 equals 0
1 XOR 0 equals 1B0 XOR 0 equals 0. That is, the same gets 0, but the difference gets 1.
For example, 10010001 (binary) ^ 11110000 (binary) equals 01100001 (binary).
5. Bit left shift operation
The bit left shift operation shifts the whole number to the left by several bits, and the vacated part 0 after the left shift. For example, an 8-bit byte variable
Byte a=0x65 (that is, binary 01100101), move it 3 bits to the left: the result of A3 is 0x0c (binary 00001100).
When performing bit and, OR, XOR operations, if the two operands are of the same type, the type of the result is the type of the Operand. For example, if you do an and operation on two int variables an and b, the type of operation result is still int. If two operations
If the type of the object is inconsistent, C# converts the inconsistent type to a consistent type, and then performs the operation.
The rules for type conversion are the same as the conversion of integer quantities in arithmetic operations.
The expression formed by the bit operator connecting the integer quantity is the bit operation expression.
(details 2)
The bitwise and operator (&)
1. Operation rules
For the two data participating in the operation, the "and" operation is performed in binary digits. If both corresponding binary bits are 1, the result value of that bit is 1, otherwise it is 0, that is:
Zero one million one.
2. Use
(1) zeroing
Operand: the bit that is 1 in the original number and 0 in the new number.
(2) take some fingers in a number to locate.
If you want to take the low (high) bytes of an integer a (2 bytes), simply bit a with the octal 37777 (177400).
(3) keep a bit of a number.
Performs a & operation with a number that takes 1 at that bit.
3. For example, the writable formula of 9'5 is as follows: 00001001 (binary complement of 9) & 00000101 (binary complement of 5) 00000001 (binary complement of 1).
Bitwise and operations are usually used to clear or retain certain bits. For example, if the high octet of an is cleared 0 and the lower octet is reserved, it can be calculated as aq255 (the binary number of 255 is 00000011111111).
Main () {
Int axiom9 and baccalaure5 c
C=a&b
Printf ("adept% d\ nb=%d\ nc=%d\ n", a dint bpenary c)
}
Bitwise or operator (|)
1. Operation rules
For the two data participating in the operation, the "OR" operation is performed in binary digits. If both corresponding binary bits are 0, the result value of that bit is 0, otherwise it is 1, that is:
0 | 0just0 | 1fug1magin1 | 0fu1magin1 | 1fug1.
2. Use
Some bit values for a data are 1.
3. For example: 9 | 5 can be written as follows: 00001001 | 00000101
00001101 (decimal 13) is visible 9 | 5: 13
Main () {
Int axiom9 and baccalaure5 c
Centra | b
Printf ("adept% d\ nb=%d\ nc=%d\ n", a dint bpenary c)
}
Third, the XOR operator (^)
Also known as XOR operator.
1. Operation rules
If the two binary digits that participate in the operation have the same sign, the result is 0 (false), and the different sign is 1 (true), that is:
0 ^ 0 = 0,0 ^ 1 = 1,1 ^ 0 = 1, 1 ^ 1 = 0.
2. Use
(1) flip the special positioning
Suppose you have 01111010, and if you want to flip it down by 4 bits, you can do a ^ operation with 00001111.
(2) with 0 ^, keep the original value
(3) Exchange two values without temporary variables
If axiom 3 is not the same as 4. If you want to exchange the values of an and b, you can use the following assignment statement:
A = a ^ b; b = b ^ a; a = a ^ b
3. For example, 9 ^ 5 can be written as follows: 00001001 ^ 00000101 00001100 (decimal 12)
Main () {
Int axi9
A = a ^ 15
Printf ("aversion% d\ n", a)
}
Fourth, "take the inverse" operator (~)
1. Operation rules
~ is a monomial (meta) operator that is used to reverse a binary number bitwise, that is, 0 to 1, 1 to 0.
2. Use
To make the lowest bit of an integer a 0, you can use: a=a&~1
3. For example, the operation of ~ 9 is: ~ (00000000001001) the result is 1111111111110110
The left shift operator (2 means to move 000001111 right to 00000011 (decimal 3). It should be noted that for signed numbers, when you move to the right, the symbol bit will move with it. When it is positive, the highest bit is 0, while when it is negative, the symbol bit is 1, and whether the highest bit is 0 or 1 depends on the compilation system. Turbo C and many systems are defined as supplement 1.
Main () {
Unsigned a,b
Printf ("input a number:")
Scanf ("d", & a)
Bachela > > 5
B=b&15
Printf ("averse% d\ tb=%d\ n", aPermie b)
}
Please look at another example!
Main () {
Char has a history of being baked.
Int p,c,d
Paira
P = (p8)
Printf ("adept% d\ nb=%d\ nc=%d\ nd=%d\ n", a recorder breco cpene d)
}
6. Right shift operator (> >)
1. Operation rules
It is used to move all the binary bits of a number to the right, and the low bit at the right end is discarded, and for unsigned numbers, the high position is filled with 0.
For signed numbers, move to the left 0 ("logical right") or 1 ("arithmetic right")
2. Use
Moving one bit to the right is equivalent to dividing by 2.
Bitwise operation assignment operator
Bit operators and assignment operators can form composite assignment operators, such as:
& =, | =, > > =, breadbasket 3
Pbit- > c | = 1
Printf ("% dforce% dpene% d\ n", pbit- > a djue pbit-> bjre pbit-> c)
}
In the above example, the bit domain structure bs is defined, and the three bit fields are a _ c Illustrates the variable bit of type bs and the pointer variable pbit that points to type bs. This means that the bit field can also use pointers.
Lines 9, 10 and 11 of the program assign values to the three bit fields, respectively. (note that the assignment cannot exceed the allowable range of the bit field.) Line 12 of the program outputs the contents of the three fields in integer format. Line 13 gives the address of the bit field variable bit to the pointer variable pbit. Line 14 uses a pointer to reassign the bit field a to 0. Line 15 uses the compound bit operator "& =", which is equivalent to: pbit- > bounded pbit-> bounded 3-bit field b has the original value of 7, and the result of bitwise sum with 3 is 3 (111-11-11-011, decimal value 3). Similarly, the compound bit operation "| =" is used in line 16 of the program, which is equivalent to: pbit- > clocked pbit-> c | 1, and the result is 15. Line 17 of the program outputs the values of these three fields as a pointer.
Type definer typedef
The C language not only provides a wealth of data types, but also allows the user to define the type specifier, that is, to allow the user to "alias" the data type. The type definer typedef can be used to accomplish this function. For example, there is an integer quantity, int b, which is described as follows: int aa,b;, where int is the type specifier of an integer variable. The complete writing of int is integer.
In order to increase the readability of the program, the integer descriptor can be defined as typedef int INTEGER with typedef, and then INTEGER can be used instead of int as the type description of integer variables. For example: INTEGER a _ r b; it is equivalent to: int a _ r b; defining arrays, pointers, structures and other types with typedef will bring great convenience, which not only makes the program writing simple but also makes the meaning more clear, thus enhancing readability. For example:
Typedef char NAME [20]; indicates that NAME is a character array type with an array length of 20.
Then you can use NAME to explain variables, such as: NAME A1 Magazine S2; completely equivalent to: char A1 [20], a2 [20], S1 [20], S2 [20]
Another example is:
Typedef struct stu {char name [20]
Int age
Char sex
} STU
Define STU to represent the structure type of stu, and then use STU to describe the structure variable: STU body1,body2
The general form of typedef definition is: typedef original type name, new type name, which contains the definition part in the Central Plains type name, and the new type name is generally expressed in uppercase, with
It's easy to tell the difference. Sometimes the function of typedef can be replaced by macro definition, but macro definition is done by preprocessing, while typedef is done at compile time, which is more flexible and convenient.
Example:
For example, there are medium color choices, 1 for red, 2 for blue, 4 with black, 8 with love.
1 0000 0001
2 0000 0010
4 0000 0100
800000 1000
If you choose 2 and 4 (both red and black), then 1 (0000 0001) or 2 (0000 0010) or 4 (0000 0100) = 7 (0000 0101)
If seven is offered to you, how do you know that you have chosen 1 and 2 and 4? The answer is: 7 and four numbers 1, 2, 4, 8, or 7, respectively, which means that someone has been selected.
For example, 7 or 2: 7, so 1 is chosen 7 or 8: 15, which is not equal to 7, so 8 is not selected, so you should know the use.
The above content is how to implement bit operation in C#. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.