C # binary, decimal, hexadecimal conversion

Source: Internet
Author: User
Tags binary to decimal decimal to binary

// Convert decimal to binary
Console. writeline (convert. tostring (69, 2 ));
// Convert decimal to octal
Console. writeline (convert. tostring (69, 8 ));
// Convert decimal to hexadecimal
Console. writeline (convert. tostring (69, 16 ));

// Convert binary to decimal
Console. writeline (convert. toint32 ("100111101", 2 ));
// Octal to decimal
Console. writeline (convert. toint32 ("76", 8 ));
// Convert hexadecimal to decimal
Console. writeline (convert. toint32 ("FF", 16 ));

In C #, you can perform logical operations on integer computing objects by bit. The meaning of logical operations by bit is that each bit of the object to be operated is obtained in sequence for logical operations. The logical operation result of each bit is each bit of the result value.

C # The supported bitwise logical operators are shown in Table 2.9.

Operator number meaning object type operation result type object count instance

~ Bit logical non-operational integer, numeric integer 1 ~ A

& Bit logic and operation 2 A & B

| Bit logic or operation 2 A | B

^ Bit 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

Bit logical non-operation is a single purpose and only one operation object. Bitwise logical non-operation performs non-operation on the value of the computing object by bitwise. That is, if a bit is equal to 0, it is converted to 1. If a bit is equal to 1, convert it to 0.
For example, if the bitwise logic is not performed on Binary 10010001, the result is equal to 01101110, which is represented in decimal:
~ 145 is equal to 110; bitwise logic is not performed on Binary 01010101, and the result is equal to 10101010. In decimal format ~ 85 equals 176.

2. Bit logic and Operation

Bitwise logic and Operations perform and perform operations on two calculation objects by bit. And calculation rules: 1 and 1 are equal to and 0 is equal to 0.
For example, 10010001 (Binary) & 11110000 equals 10010000 (Binary ).

3. Bit logic or operation

Bitwise logic or operation performs or on two operation objects by bit. The rule of the or operation is: 1, 1, or 0 equals 1,
0 or 0 is equal to 0. For example, 10010001 (Binary) | 11110000 (Binary) equals 11110001 (Binary ).

4. Bit logical exclusive or operation

Bitwise logical exclusive or operation performs bitwise exclusive or operation on Two Computing objects. The rule for an exclusive or operation is: 1 exclusive or 1 equals 0,
1 or 0 equals 1, 0, or 0 equals 0. That is, 0 is the same, and 1 is different.
For example, 10010001 (Binary) ^ 11110000 (Binary) equals 01100001 (Binary ).

5. bitwise left shift operation

The bitwise left shift operation shifts the entire number to several places by bit, and the portion left after the shift is left is 0. For example, an 8-bit BYTE Variable
Byte a = 0x65 (that is, 01100101 of the binary), shifts it three places to the left: the result of a <3 is 0x27 (that is, 00101000 of the binary ).

6. Right Shift operation

The bitwise right shift operation shifts the entire number to several places by bit, and the portion left blank after the right shift is set to 0. For example, an 8-bit BYTE Variable
Byte a = 0x65 (both (Binary 01100101) shifts it three places to the right: the result of a> 3 is 0x0c (Binary 00001100 ).

When bitwise AND, Or, exclusive or operations are performed, if the types of the two operation objects are the same, the type of the operation result is the type of the operation object. For example, if two int variables A and B are compared, the type of the operation result is still Int. If two operations

If the object type is inconsistent, C # converts the type of the inconsistent type to a consistent type and then performs an operation.
The type conversion rules are the same as those for integer conversions in arithmetic operations.

A bitwise operator is a bitwise expression that connects integer values.

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.