


And operation&
Example:
3&5
Solution: The two’s complement of 3 is 11, and the two’s complement of 5 is 101, 3&5 is 011&101, Let’s look at the hundreds digit first (actually it is not the hundreds digit, this is just to facilitate understanding). There is a 0 and a 1. According to (1&1=1, 1&0=0, 0&0=0, 0&1=0), we know that the hundreds digit should be 1, and the tens digit is also the same. The number 1&0=0, the number in the ones place is 1&1=1, so the final result is 1. (There should be one step after this, because the value we get now is just the complement of the answer we are looking for, but because of the positive number The complement is itself, so it is omitted. However, the last step cannot be omitted in the following example).
-1&-2
Solution: The complement of -1 is 11111111, and the complement of -2 is 11111111. The code is 11111110, 11111111&11111110. The result is: 11111110. This is the complement code. Then the original code is converted to 100000010 (the method of converting a negative number to the original code is to subtract one and negate it). The final conversion to decimal is -2.
-2&6
Solution: -2's complement is 11111110, 6's complement is 110, 11111110&110, which is 11111110&00000110 (the purpose of writing this is to allow beginners to better understand bitwise operations), follow the above method to get The result is: 110, converted to decimal is 6.
Tips: Use bitwise AND to change the last digit of any binary number to 0, which is X&0.
eg:
a = 5 b = 3 print a & b
Result: 1
How is this calculated? It is actually calculated through the binary system of a and b.
# a 的 b 的二进制 # 0*2**3 + 1*2**2 + 0*2**1 + 1*2**0 # 开始与运算 a = 0101 b = 0011
Result: 0001
The AND operation is to compare the binary numbers of a and b. If the digits are both 1, it will be counted as 1. If you don’t want the same or both If it is 0, it is counted as 0. Then convert the answer from binary to decimal.
OR operation|
Example:
4|7
Solution: The calculation rule of bitwise union is very similar to that of bitwise AND, but the logic is changed. operator, the rule of union is: 1|1=1,1|0=1, 0|0=0. 4|7 converted to binary is: 100|111=111. Binary 111 is 7 in decimal.
Tips: Using bitwise addition, you can change the last digit of any binary number to 1, which is X|1.
eg:
a = 5 b = 3 print a | b
Result: print 7
a = 0101 b = 0011
a | bThe result is: 0111
The OR operation is exactly the opposite of the AND operation. If the bit If the number is not 0, it is counted as 1, otherwise it is counted as 0.
XOR operation
Method: Bit addition, special attention should be paid to not carrying.
Example:
2^5
Solution: 10^101=111, the decimal result of binary 111 is 7.
1^1
Solution: 1+1=0. (Originally binary 1+1=10, but carry is not allowed, so the result is 0)
-3^4
Solution: The complement of -3 is 11111101, the complement of 4 is 100 (that is, 00000100), 11111101^00000100=11111101, the complement of 11111101 converted to the original code is 1000111, That is -7 in decimal.
a = 5 b = 3 print a ^ b
Result: 6
a = 0101 b = 0011
a ^ b The result is 0110
If the number of digits in the XOR operation is not the same, it is counted as 1, otherwise it is counted as 0.
Left shift and right shift
1. Left shift operator Method: Move N bits to the left.
Example:
3Solution: 11 moves two places to the left to become 1100, which is 12.
2. Right shift operation Symbol >>
Method: X>>N Move the binary number corresponding to a number :11 moves two places to the right and becomes 0.
10>>1
Solution: The binary number of 10 is 1010, and moving one place to the right is 101, which is 5.
a = 5 b = 2 print a << bThe result is 20
##
a = 0101 b = 2
a The bit shift operation will move the binary number to the left or right. As shown above, it moves 2 units to the left.
For more articles related to summary of decimal bitwise operations and calculations in Python, please pay attention to the PHP Chinese website!

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

Python provides a variety of ways to download files from the Internet, which can be downloaded over HTTP using the urllib package or the requests library. This tutorial will explain how to use these libraries to download files from URLs from Python. requests library requests is one of the most popular libraries in Python. It allows sending HTTP/1.1 requests without manually adding query strings to URLs or form encoding of POST data. The requests library can perform many functions, including: Add form data Add multi-part file Access Python response data Make a request head

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

PDF files are popular for their cross-platform compatibility, with content and layout consistent across operating systems, reading devices and software. However, unlike Python processing plain text files, PDF files are binary files with more complex structures and contain elements such as fonts, colors, and images. Fortunately, it is not difficult to process PDF files with Python's external modules. This article will use the PyPDF2 module to demonstrate how to open a PDF file, print a page, and extract text. For the creation and editing of PDF files, please refer to another tutorial from me. Preparation The core lies in using external module PyPDF2. First, install it using pip: pip is P

This tutorial demonstrates how to leverage Redis caching to boost the performance of Python applications, specifically within a Django framework. We'll cover Redis installation, Django configuration, and performance comparisons to highlight the bene

Natural language processing (NLP) is the automatic or semi-automatic processing of human language. NLP is closely related to linguistics and has links to research in cognitive science, psychology, physiology, and mathematics. In the computer science

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
