C # bitwise operators

Source: Internet
Author: User
Tags bitwise bitwise operators

C # bitwise operators &, |, ^, <,> on Friday, August 01, 2008
We know that any information in the computer is saved in binary form. The bit operator is the logarithm.
The bitwise operators in the C # language are

& Unlike | or ^ Or ~ Complement <left shift> right shift

The complement operation only has one operand, while other bitwise operators both have two operands.
The operand of the overflow bitwise operator is an integer or any other type that can be converted to an integer.
And operations
The rule for operations on operands in binary bits is
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
This indicates that, except when the two bits are both 1 and the calculation result is 1, the calculation result is 0.
Such as 2 and 10.
The binary value of 2 indicates 00000010.
The binary value of 10 indicates 00001010.
And Calculation Result 00000010
1. The result of 2 & 10 is 2.
Or operation
The rule for operations on operands in binary bits is
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
This indicates that, except when both bits are 0 or the operation result is 0, the OR operation result is a ratio of 1.
Such as 2 and 10 or
The binary value of 2 indicates 00000010.
The binary value of 10 indicates 00001010.
Or calculation result 00001010
Therefore, the result of 2 | 10 is 10.
Exclusive or operation
The rule for operations on operands in binary bits is
0 ^ 0 = 0
0 ^ 1 = 0
1 ^ 0 = 0
1 ^ 1 = 1
This indicates that when the two phases are at the same time, the XOR operation result is 0, and the XOR operation result is 1, for example
2 and 10 perform exclusive or operations
The binary value of 2 indicates 00000010.
The binary value of 10 indicates 00001010.
Result 00001000 of the XOR operation
So the result of 2 ^ 10 is 8.
Complement operation
If the complement operation is performed on each bit of the operand, if the result is 10
The binary value of 10 indicates 00001010.
And Calculation Result 11110101
The specific value of the binary value is related to the data type. The following example clearly illustrates this point.
Program list 7-5

using System;class Test{public static void Main() {short a = 10;ushort b = 10;int c = 10;uint d = 10;Console.WriteLine(~10);Console.WriteLine("short: {0}",~a);Console.WriteLine("ushort: {0}",~b);Console.WriteLine("int: {0}",~c);Console.WriteLine("uint: {0}",~d);}}

The correct output is
-11
Short:-11
Ushort:-11
INT:-11
Uint: 4294967285
Shift operation
The left-shift operation removes the operands from the left-shifted high position by bit. The order of the low position is 0. For example, the binary value of 10 is
00001010 shifts one digit to 00010100 20 shifted to 00101000 40
If the operand X is Int or long, the low position of X is discarded.
Shift right one by one. If X is a non-negative number, the highest bit is set to zero. If X is a negative number, the highest bit is set to 1 and when x
When the type is uint or ulong, the low position of X will be discarded. Other orders shift right to 0.
For example
Program list 7-6

using System;class Test{public static void Main() {int x = 16;Console.WriteLine(x);int y = x>>2;Console.WriteLine(y);y = y>>2;Console.WriteLine(y);y = y>>2;Console.WriteLine(y);}}

The output of the above program is
16
4
1
0
If you set the initial value of X to-16, the output of the program is
-16
-4
-1
-1

C # bitwise operators

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.