C # Bitwise operations

Source: Internet
Author: User

in C #, you can perform a bitwise logical operation on an integer operand. The meaning of the bitwise logical operation is that each bit of the operand is taken sequentially, and the logical operation of each bit is the result of each bit of the result value. The bit logical operators supported by C # are shown in table 2.9.
Operation symbols Significance Operand type Type of operation result Number of objects Instance
~ Bitwise logical non- operation Integral type, character type Integral type 1 ~a
& Bit logic and operations 2 A & B
| Bit logic or operations 2 A | B
^ Bitwise logical XOR operation 2 a ^ b
<< Bitwise LEFT Shift operation 2 A<<4
>> Bitwise RIGHT Shift operation 2 A>>2
1, bit logical non-operation A bitwise logical non-operation is single-purpose, with only one operand. Bitwise logical non-operation bitwise pairs the value of an operand into a non-operation, i.e. if one is equal to 0, it is converted to 1, and if one is equal to 1, it is converted to 0. For example, the binary 10010001 is a bitwise logical non-operation, the result is equal to 01101110, in decimal notation is:The ~145 equals 110, and the binary 01010101 is a bitwise logical non-operation, and the result is equal to 10101010. In decimal notation is ~85 equals 176. 2 . Bit logic and Operation Bitwise Logic and operations perform bitwise AND operations on two operands. Rules with operations: 1 and 1 equals 1, 1 and 0 equals 0. For example: 10010001 (binary) &11110000 equals 10010000 (binary). 3. bit logic or arithmetic Bitwise logic or operation bitwise OR operation of two operands. Or the rule of operation is: 1 or 1, etc. 1, 1 or 0 equals 1,0 or 0 equals 0. such as 10010001 (binary) | 11110000 (binary) equals 11110001 (binary). 4 , bit logic xor operation A bitwise logical XOR operation makes two operands bitwise XOR. The rule for XOR operation is: 1 xor or 1 equals 0,1 xor 0 equals 1, 0 xor or 0 equals 0. That is, the same 0, different 1. For example: 10010001 (binary) ^11110000 (binary) equals 01100001 (binary). 5 . Bitwise left SHIFT Operation The bitwise left shift operation shifts the entire number of digits to the left by a number of bits, left-shifted to the vacated portion 0. For example: 8-bit byte variablebyte a=0x65 (that is, binary 01100101), shifts it to the left 3 bits: The result of A<<3 is 0x27 (that is, the binary 00101000). 6 . Bitwise RIGHT SHIFT operation The bitwise Right SHIFT operation shifts the entire number of digits to the right by several bits, and the vacated portion of the right shift is 0. For example: 8-bit byte variableByte a=0x65 (both (binary 01100101)) shifts it to the right 3 bits: The result of A>>3 is 0x0c (binary 00001100). The type of the result of an operation is the type of the operand , if the type of the two operands is the same when the bitwise AND, OR, XOR, or operation is performed. For example, for two int variables A and B, the type of the result of the operation is the int type. If two operationsobject does not have the same type, C # converts the inconsistent type into a consistent type and then operations. The rule of type conversion is the same as the conversion of integral type in arithmetic operation. An expression that is connected by a bitwise operator to an integral type is a bitwise-Operation expression.     (detailed 2)

The bitwise-AND operator (&)
1. Operation rules
Participate in the operation of the two data, the "and" operation in binary, if the two corresponding binary is 1, then the result value of the bit is 1, otherwise 0, that is:
2. Use
(1) Zeroing
Operand: The number of the original digit is 1, the corresponding bit in the new number is 0.
(2) Take some of the points in a number to locate.
If you want to take a low (high) byte of an integer A (2 bytes), you can simply place a and octal 377 (177400) in a bitwise.
(3) A person who retains a certain number.
With a number of & operations, this number takes 1 in that bit.
3, for example: 9&5 can be written as follows: 00001001 (9 twos complement) &00000101 (5 of the Twos complement) 00000001 (1 of the twos complement) is visible 9&5=1.
Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, A's high eight bits clear 0, reserved low eight bits, can be used as a&255 operation (255 binary number is 0000000011111111).
Main () {
int a=9,b=5,c;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);

Second, bitwise OR operator (|)
1. Operation rules
Participate in the operation of two data, in binary for the "or" operation, if the two corresponding binary is 0, then the result value of the bit is 0, otherwise 1, that is:
2. Use
Certain bits of a data are set to 1.
3. For example: 9|5 can be written as follows: 00001001|00000101
00001101 (decimal 13) Visible 9|5=13
Main () {
int a=9,b=5,c;
printf ("a=%d\nb=%d\nc=%d\n", a,b,c);
Third, Xor operator (^)
Also called the XOR operator.
1. Operation rules
The result is 0 (false) if the two binary numbers of the operation are joined, and the XOR is 1 (true), i.e.:
2. Use
(1) Turn specific bits upside down
Suppose you have 01111010 and want to flip it down by 4 bits, you can do it with 00001111 ^.
(2) with 0-phase ^, retain the original value
(3) Exchange two values without temporary variables
If a=3,b=4. To swap the values of a and B, you can use the following assignment statements:
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 a=9;
printf ("a=%d\n", a);

Four, "Inverse" operator (~)
1. Operation rules
~ is a monocular (meta) operator that is used to reverse the bitwise of a binary number, which will change from 0 to 0.
2. Use
The lowest bit of an integer A is 0 and can be used:a=a&~1;
3, for example, the operation is: ~ (0000000000001001) The result is: 1111111111110110

Five, left shift operator (<<)
1. Operation rules
Used to move a number of each binary all left several bits, right 0, high-left after overflow, discard does not work.
2. Use
Shift left one is equivalent to multiplying by 2
3, for example: set a=15,a>>2 to move 000001111 right to 00000011 (decimal 3). It should be stated that, for the signed number, when moving right, the symbol bit is moved along with it. When positive, the highest bit is 0, while negative, the sign bit is 1, the highest bit is 0 or the complement 1 depends on the requirements of the compilation system. Turbo C and a number of systems are required to complement 1.
Main () {
unsigned A, B;
printf ("Input a number:");
scanf ("%d", &a);
printf ("a=%d\tb=%d\n", A, b);
Please look at one more example!
Main () {
Char a= ' A ', b= ' B ';
int p,c,d;
p= (p<<8) |b;
C= (P&AMP;0XFF00) >>8;
printf ("a=%d\nb=%d\nc=%d\nd=%d\n", a,b,c,d);

Six, right shift operator (>>)
1. Operation rules
Used to move a number of each binary all right to a number of bits, moved to the right end of the low is discarded, on the unsigned number, high 0;
For signed numbers, move left in 0 ("Logical right Shift") or 1 ("Arithmetic right Shift")
2. Use
Shift right one is equivalent to dividing by 2.

Seven-bit arithmetic assignment operators
The bitwise operator and the assignment operator can form compound assignment operators, such as:
Eight, different lengths of data for bit operations
If the length of the two data is different, when the bit is performed (for example: A&b, and A is long and b is int), the system will align the two on the right side. If B is a positive number, the left 16 bits fill 0, and if B is negative, the left end should fill 1, and if B is an unsigned integer, it fills 0.

Bit field

While some information is stored, it does not need to occupy a full byte, only a few or one bits. For example, when storing a switching volume, there are only 0 and 12 states, with one binary. In order to save storage space and make processing simple, C language also provides a data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in one byte into several different regions and describe the number of bits per region. Each domain has a domain name that allows operations to be performed by domain name in the program. This allows you to represent several different objects in a bits field of one byte. The definition of a bit field and the description of a bit-field variable are similar to the structure definition, in the form of:
struct bit domain structure name
{bit field List};
Where the list of bit fields is in the form: type specifier bit domain name: bit field length

For example:
struct BS
int a:8;
int b:2;
int c:6;
The description of a bit-field variable is the same as the structure variable description. Can be defined by the first description, at the same time define the description or direct description of the three ways. For example:
struct BS
int a:8;
int b:2;
int c:6;
Indicates that data is a BS variable, which accounts for two bytes. Where bit domain A occupies 8 bits, bit domain B is 2 bits, bit field C is 6 bits. There are several explanations for the definition of bit fields:

1. A bit field must be stored in the same byte and cannot span two bytes. If one byte has enough space left to hold another domain, the bit field should be stored from the next cell. You can also intentionally make a field start from the next unit. For example:
struct BS
unsigned a:4
unsigned:0/* airspace */
Unsigned b:4/* Start storage from the next unit */
unsigned c:4
In this bit domain definition, a occupies the first byte of 4 bits, the latter 4 bits fill 0 means not to use, B starts with the second byte, occupies 4 bits, and C occupies 4 bits.

2, because bit fields are not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is, it cannot exceed the 8-bit binary.

3, bit domain can be no bit domain name, when it is only used to fill or adjust the location. Nameless bit fields are not available. For example:
struct k
int a:1
Int:2/* The 2-bit cannot be used */
int B:3
int C:2
From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.

The use of bit fields in bit fields is the same as the use of the struct members, and the general form is: bit field variable name • Bit domain name field allows output in various formats.
Main () {
struct BS
unsigned a:1;
unsigned b:3;
unsigned c:4;
} bit,*pbit;
printf ("%d,%d,%d\n", bit.a,bit.b,bit.c);
printf ("%d,%d,%d\n", pbit->a,pbit->b,pbit->c);

In the example program, the bit-domain structure BS is defined, and the three-bit domain is a,b,c. The variable bit for the BS type and pointer variable pbit to the BS type are described. This means that a bit field can also use pointers.
The 9, 10, and 113 lines of the program assign values to the three bit fields respectively. (Note that the assignment cannot exceed the allowed range of this bit field) the 12th line of the program outputs the contents of three fields in an integer format. The 13th line sends the address of bit field variable bit to pointer variable pbit. The 14th row assigns a value to bit field A as a pointer to 0. Line 15th uses the compound bitwise operator "&=", which is equivalent to: pbit->b=pbit->b&3 bit field B has a value of 7, and 3 for bitwise AND operation results of 3 (111&011=011, decimal value 3). Similarly, the 16th line of the program uses the compound bit operation "|=", equivalent to: pbit->c=pbit->c|1 its result is 15. The 17th line of the program outputs the values of these three fields in a pointer manner.

Type-defined typedef

The C language not only provides a rich data type, but also allows the user to define the type descriptor themselves, which means that the user is allowed to "alias" the data type. The type definition typedef can be used to complete this function. For example, there is an integer, A, B, which is described as follows: int aa,b; where int is the type specifier for an integer variable. int is the complete notation of integer,

In order to increase the readability of the program, the integer specifier can be defined with a typedef as: TypeDef int INTEGER which can be used as an integer to replace int as the type description of the integer variable. For example: INTEGER A, B; it is equivalent to: int A, B; The use of typedef to define arrays, pointers, structures and other types will bring great convenience, not only makes the program simple and makes the meaning more explicit, thus enhancing readability. For example:
typedef char NAME[20]; Indicates that name is a character array type with an array length of 20.
You can then use the name to describe the variable, such as: Name A1,a2,s1,s2; exactly equivalent to: Char a1[20],a2[20],s1[20],s2[20]
Another example:
typedef struct stu{char name[20];
int age;
char sex;
} STU;
Define the structure type that STU represents STU, and then use STU to describe the structure variable: STU body1,body2;
The general form of typedef definition is: TypeDef primitive type name new type name where the original type name contains the definition part, the new type name is generally capitalized, in order to
Easy to distinguish. Macro definitions are sometimes used instead of TypeDef, but macro definitions are done by preprocessing, and TypeDef is done at compile time, which is more flexible and convenient.

For example, there are medium color options, 1 for red, 2 for blue, 4 with table black, 8 with confession
1=0000 0001
2=0000 0010
4=0000 0100
8=0000 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 7 is offered to you, how do you know if you have chosen 1 and 2 and 4? The answer is: 7 with four number 1,2,4,8, or the result is 7, it means that one is selected
such as: 7 or 2=7, so 1 is selected 7 or 8=15, not equal to 7 oh, so 8 is not selected, so you should know the use of it

The specific bits are calculated as follows:

Operation name Meaning
Operand type
Number of operand instances ~ bit logical non-operation
Integer or character integral type
1 ~a & bit logic and operation integer or character type
Integral Type 2 A&b | Bit-Logical OR op-integer or character-type integer 2 a|b^
Bitwise logical XOR integer or character integer 1 ^a << bitwise left-shift operation integer or character integer 2 a<<4 >> Bitwise-Shift-op integer or character-type integer 2 a>>2

C # Bitwise operations

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.