第一节
1 介绍了Python的二进制
2 我们利用0bxx来表示一个二进制,比如1 = 0b1,2 = 0b10等等
第二节
1 介绍了Python的函数bin(x),主要是用来求10进制数x的二进制值
2 比如bin(2)为0b10
3 练习:打印出2~5之间的数的二进制值
print bin(1)
for i in range(2,6):
print bin(i)
第三节
1 介绍了Python的int()函数
2 最普通的int()函数的用法是int(str)是把一个字符串转化成一个数字
3 但是int(str,base)可以用来把base进制下的值转化为10进制,比如int("0b11",2)得到的值就是3
4 举例:
print int("1",2) >> 1
print int("10",2) >> 2
print int("111",2) >> 7
print int("0b100",2) >> 4
print int(bin(5),2) >> 5
print int("11001001",2) >> 201
第四节
1 介绍了Python的左移和右移运算
2 位移运算的举例
Left Bit Shift (<<)
0b000001 << 2 = 0b000100 (1 << 2 = 4)
0b000101 << 3 = 0b101000 (5 << 3 = 40)
Right Bit Shift (>>)
0b0010100 >> 3 = 0b000010 (20 >> 3 = 2)
0b0000010 >> 2 = 0b000000 (2 >> 2 = 0)
3 练习:把变量的shift_right的右移两位,shift_left的值左移两位
shift_right = 0b1100
shift_left = 0b1
# Your code here!
shift_right >>= 2
shift_left <<= 2
print bin(shift_right)
print bin(shift_left)
第五节
1 介绍了Python的另外一种位运算&
2 比如a&b,就是两个位都是1为1,否则为0
a: 00101010 42
b: 00001111 15
===================
a & b: 00001010 10
3 练习:把二进制0b1110&0b101的结果用二进制打印出来
print bin(0b1110&0b101)
第六节
1 介绍了Python的另外一种位运算|
2 比如a|b,就是如果两个位中有1就为1,否则为0
a: 00101010 42
b: 00001111 15
================
a | b: 00101111 47
3 练习:把二进制0b1110|0b101的结果用二进制打印出来
print bin(0b1110|0b101)
第七节
1 介绍了Python的另外一种位运算^
2 比如a^b,就是如果两个位中如果是不同的那么就为1,否则为0
a: 00101010 42
b: 00001111 15
================
a ^ b: 00100101 37
3 练习:把二进制0b1110^0b101的结果用二进制打印出来
print bin(0b1110^0b101)
第八节
1 介绍了Python的另外一种位运算~
2 比如~a的结果就是a的相反数在减一,对于a是正数或负数都满足
3 举例
print ~-4 >> 3
print ~-3 >> 2
print ~-1 >> 0
print ~0 >> -1
print ~1 >> -2
print ~2 >> -2
print ~3 >> -2
分享到:
相关推荐
Laravel开发-laravel-bitwise-trait 在任何类上使用位运算符的简单特性
level5-04-bitwise-operations-jaLeague:level5-04-bitwise-operations-jaLeague由GitHub Classroom创建
level5-04-bitwise-operations-eliyahubowers:GitHub Classroom创建的level5-04-bitwise-operations-eliyahubowers
OpenCV-Python图像位与运算bitwise_and函数详解.rar
华为一个WCDMA路测仪表说明,有部分界面设计和功能参考价值,希望对大家有用
Matlab practical guide instance M file
4.5 Bitwise Operators 4.6 Relational Operators 4.7 Logical Operators 4.8 The Conditional Operator The C Cheat Sheet Revision 1 Copyright ? 2000 Andrew Sterian iv 4.9 The Comma Operator 4.10 ...
Byte to be bitwise manipulated byte -> 10101010 Bitmask to switch ON bit 3 mask on -> 00000100 Use bitwise OR | to switch on byte | on -> 10101110 Bitmask to switch OFF bit 2 mask off -> 11111101 Use ...
Bitwise是一个教育项目,能让我们从头开始为计算机创建软件/硬件堆栈
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...
opencv按位计算 配合博文使用
js-bitwise-visualizer:一个简单的应用程序,用于可视化JavaScript中的按位操作
New Features Added to Python 2.7 Maintenance Releases PEP 434: IDLE Enhancement Exception for All Branches PEP 466: Network Security Enhancements for Python 2.7 Acknowledgements What’s New in ...
2016中的第三人称库编写,并在Python27中进行了绘图和数据处理。 特定文件夹具有相应的readme.txt以获取说明。 请参阅本文以获取有关算法和实现的确切详细信息 为纸。 抽象的: 元胞自动机(CA)理论是一个离散模型...
2.2.4 Bitwise Operators 17 2.2.5 Assignment 17 2.2.6 Mnemonics 17 2.2.7 Constants 18 2.3 Method of Describing Bit Stream Syntax 18 2.4 Requirements 20 2.4.1 Coding Structure and Parameters 20 2.4.2 ...
JMP - Jump to Address JNB - Jump if Bit Not Set JNC - Jump if Carry Not Set JNZ - Jump if Accumulator Not Zero JZ - Jump if Accumulator Zero LCALL - Long Call LJMP - Long Jump MOV - Move Memory MOVC -...
—如何使用import bitwiseRotation from 'bitwise-rotation' ;// Set the bit-width/length; max of `32`, min of `0`.const rotationObject = bitwiseRotation ( 8 ) ;// Returns an object with rotate right (`...
2.2.4 Bitwise Operators 17 2.2.5 Assignment 17 2.2.6 Mnemonics 17 2.2.7 Constants 18 2.3 Method of Describing Bit Stream Syntax 18 2.4 Requirements 20 2.4.1 Coding Structure and Parameters 20 2.4.2 ...
Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops -...