C # basic usage of bitwise operators,

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

C # basic usage of bitwise operators,

Bitwise operators include: | bitwise OR, & bitwise 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


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.