search
HomeBackend DevelopmentPython TutorialSummary of decimal bitwise operations and calculations in Python

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 << b

The 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools