C # binary, decimal, hexadecimal

Source: Internet
Author: User
Tags bitwise

//decimal into binary
Console.WriteLine (Convert.ToString (69, 2));
Decimal Turn octal
Console.WriteLine (Convert.ToString (69, 8));
Decimal Turn hex
Console.WriteLine (Convert.ToString (69, 16));

//binary Turn decimal
Console.WriteLine (Convert.ToInt32 ("100111101″, 2)");
Octal goto Decimal
Console.WriteLine (Convert.ToInt32 ("76″, 8)");
Hexadecimal goto Decimal
Console.WriteLine (Convert.ToInt32 ("FF", 16));

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 symbol meaning operand type operand number instance of result type Object

~ Bit logic non-op integer, character integer 1 ~a

& Bit logic and Operations 2 A & B

| Bit logic or Operation 2 A | B

^ bit logic xor 2 a ^ b

<< left-shift Operation 2 A&LT;&LT;4

>> Right-shift operation 2 A&GT;&GT;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 variable
Byte 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 variable
Byte 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 operations
Object 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.

C # binary, decimal, hexadecimal

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.