The foundation is the top priority ~ Bitwise operations

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

Back to directory

Bit operations in C #, VB or in the T-SQL have support for it, bit operations that calculate each of the numerical type,ProgramYou can use decimal, hexadecimal, and binary to perform bitwise operations on it. In fact, no matter which hexadecimal type you use, the computer converts it to binary (0, 1) at the end) because binary is simple.

The following table describes bit operations.

Operator number Bitwise operators Calculation object type Calculation Result type Object operations Instance
~ NonOperation Integer Integer 1 ~ X
& AndOperation 2 X & Y
| OrOperation 2 X | y
^ Exclusive orOperation 2 X ^ y
< Move leftOperation 2 X <2
> Right ShiftOperation 2 X> 2

In general, in this bitwise operation, our operands are mostly used for the n result of 2, such as 64,128, 16, 32 ..., why? Because these numbers are converted to binary

In particular, take a lookCode, Huh

1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000

At that time, they are only 1 in the highest bit, and 0 in the other bit. In this way, these numbers will have some properties when performing bitwise operations. Let me summarize:

Decimal Numbers that meet the power of N2:

1. the operation can be used to determine whether a certain number exists in another number:

 
10&2 //Result 2. If the value is greater than 0, operand 2 exists in operand 1, which is 10 = 8 + 2.

2 or operation can be used to add two numbers together

 
8|2 //The result is 10, 10 | 2 or 10. You can convert it to binary. (1000 | 10 is 1010, while 1010 | 10 is 1010, and 10 | 4, the result is 14.

3. When used together with operations, an element can be removed from a set.

4&(~2)//The result is 4, 4 does not contain 2, so the original value 4 is returned directly.3&(~2)//The result is 1, 3 is composed of 1 and 2, so the original value is 1 after removing 2.

4 left shift operation. Each shift of 1 is equivalent to a multiplication of 2

 
4<2//Result 16

5. Move the Right to the destination. Each shift of 1 is equivalent to dividing 2

 
4>2 //Result 1

Summary:

Sometimes, what we see in books and on the Internet is not necessarily correct. Sometimes, we are asked to open the computer to verify it, huh, huh!

Sometimes, when you convert decimal to binary, you can program things!

Thank you for reading this article!

Back to directory

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.