C # basic usage of bitwise operators

Source: Internet
Author: User
Tags binary to decimal bitwise operators decimal to binary integer numbers

Bitwise operators include: | bitwise OR or, & bitwise AND and, ^ bitwise XOR or XOR ,~ Not, <left shift,> right shift, and so on. This article describes the application of the bitwise operator in C. It mainly includes:

 

○ Hexadecimal conversion
※Convert decimal to binary.
※Convert binary to decimal.
○ | Bitwise OR operator
○ & Bitwise AND Operators
○ ^ Bitwise OR operator
※^ Returns the number of two equal or equal bits.
※^ Bitwise XOR encryption is used.
○ ~ Inverse Operators
○ X <n left shift operator
○ X> N right shift operator

 

Hexadecimal conversion

Because bitwise operations are performed on a binary basis, hexadecimal conversion is the prerequisite for bitwise operations.

 

□Convert decimal to binary

For example, to convert the decimal number 783 into a binary number, follow the following 10 steps.


Concatenate the remainder from bottom to top, that is, the binary value of 783, that is, 1100001111.

 

In C #, if it is of the int16 type, it indicates that there is a 16-bit integer number. For 783, if it is expressed as int16, if it is less than 16 bits, it is necessary to add 0 in front, that is, to add 6 0 in 1100001111, the complete expression is: 0000001100001111. In the same way, if it is int32 type, It means there are 32-bit integer numbers.

 

How can I convert-783 to a binary value of the int16 type?

→We know that the binary value of positive number 783 expressed as int16 is: 0000001100001111
→ Invert at each position, that is, if it is 1, it becomes 0, and vice versa. After reversing, it becomes: 1111110011110000.
→Add 1 to 1111110011110001

 

□Convert binary to decimal

For example, to convert int16 binary number 0000000100010110 to decimal number, go through the following 16 steps.

From the right to the left, multiply by the N power of 2 in turn.

Add all the results together:
0 + 2 + 4 + 0 + 16 + 0 + 0 + 256 + 0 = 2 + 4 + 16 + 256 = 278
Therefore, the decimal number of 0000000100010110 is 278.

 

How can we convert a negative binary number of the int16 type into a decimal number?

For example, there is a binary number: 1111111111010011 (int16 type, the first is the number 1 to indicate a negative number, and the first is the number 0 to represent a positive number)
→ The reverse direction at each position is: 0000000000101100
→ After calculation, the resulting decimal number is: 44
→ Add 1 to get: 45
→ Convert it to a negative number and get:-45.

 

| Bitwise OR operator

Assume that the Decimal Numbers 38 and 53 are bitwise OR calculated.
→After calculation, the Binary Expression of the decimal number 38 is 00100110, And the Binary Expression of the decimal number 538 is 00110101.
→For | bitwise OR operator, if one of the two binary numbers is 1, the result is 1.

→ Convert the obtained 00110111 to decimal: 55

 

If C # is used:

byte result = 38 | 53;

 

& Bitwise AND operator

Assume that the decimal number is 76 and 231 in bitwise AND calculation.
→After calculation, the Binary Expression of the decimal number 76 is 01001100, And the Binary Expression of the decimal number 231 is 11100111.
→For & bitwise AND operators, the result is 0 if one of the two binary numbers is 0.

→ Convert the obtained 01000100 to the decimal value: 68.

 

If C # is used:

byte result = 76 & 231;

 

^ Bitwise OR operator

Assume that decimal numbers 138 and 43 are bitwise AND calculated.
→After calculation, the Binary Expression of decimal number 138 is 10001010, And the Binary Expression of decimal number 43 is 00101011.
→For the ^ bitwise OR operator, when the binary numbers of the two operators are different, the result is 1.

→ Convert the obtained 10100001 to decimal: 161.

 

If C # is used:

byte result = 138 ^ 43 ;

 

□Example 1: Use ^ bitwise XOR or exchange 2 numbers

int x = 4; 
int y = 6;
x ^= y;
y ^= x;
x ^= y;
Console.WriteLine(x); //6
Console.WriteLine(y); //4

 

The above process is as follows:
(1) x ^ = Y, equivalent to X = x ^ y
The binary value of X is: 00100.
The binary value of Y is: 00110.
Returns 00010 if X is different from Y.
After X is converted to decimal, X is: 2

 

(2) y ^ = x, equivalent to Y = y ^ x
The binary value of Y is: 00110.
The binary value of X is: 00010.
Y and X are different or, and the result is: 00100
After converting y to decimal, Y is: 4

 

(3) x ^ = Y, equivalent to X = x ^ y
The binary value of X is: 00010.
The binary value of Y is: 00100.
Returns 00110 if X is different from Y.
After X is converted to decimal, X is 6

 

□Example 2: Use ^ bitwise XOR or for encryption

String MSG = "this is the string I want to encrypt ";
string k = "mypassword";
for(int i = 0; i < msg.Length; i++)
{
    sb.Append((char)(msg[i] ^ k[i % k.Length]));
}
Console.WriteLine(sb.ToString());

 

~ Inverse Operators

Assume that the decimal number 52 is used for Inverse calculation.
→After calculation, the Binary Expression of the decimal number 52 is 00110100
→ ~ For the inverse operator, the binary number is reversed by bit, and 0 is changed to 0.

→ Convert the obtained 11001011 to decimal: 203.

 

If C # is used:

byte result = ~52;

 

X <n left shift operator

Each person moves n places to the left, and the right vacancy position is 0.
Assume that the decimal number is 154 shifted to the left.

 

If C # is used:

byte b1 = 154;
byte b2 = (byte)b1 << 1;
Console. writeline (B2); // The result is 52, and the binary value is 00110100.

 

X> N right shift operator

Each person moves n places to the right, and the left vacant position is 0.
Assume that the decimal number 155 is shifted to the right.

 

If C # is used:

byte b1 = 155;
byte b2 = (byte)(b1 >> 1);
Console. writeline (B2); // result: 77


Summary:
| For bitwise OR operators, if one of the two binary numbers is 1, the result is 1.
& For bitwise AND operators, if one of the two binary numbers is 0, the result is 0.
^ For bitwise OR operators, when the binary numbers of the two operators are different, the result is 1.
~ For the inverse operator, the binary number is reversed by bit, and 0 is changed to 0.
X <n left shift operator. Each operator moves N bits to the left, and the right vacant position is supplemented by 0.
X> N shift right operator. Each operator moves n places to the right, and the left vacant position is 0.

 

References:
Swapping of variables without third variable using XOR logic
Understand how bitwise operators work
Http://baike.baidu.com/view/9895739.htm? Fr = Aladdin

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.