当前位置:网站首页>实例054:位取反、位移动
实例054:位取反、位移动
2022-08-10 22:20:00 【懒笑翻】
题目:取一个整数a从右端开始的4〜7位。
程序分析:可以这样考虑: (1)先使a右移4位。 (2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4) (3)将上面二者进行&运算。
代码:
a=int(input('输入一个数字: '))
b=0 # 0
b=~b # 1
b=b<<4 # 10000
b=~b # 1111
c=a>>4
d=c&b
print('a:',bin(a))
print('b:',bin(b))
print('c:',bin(c))
print('d:',bin(d))
运行结果:
边栏推荐
猜你喜欢
The Thread State,
3598. 二叉树遍历(华中科技大学考研机试题)
诺诚健华通过注册:施一公家族身价15亿 高瓴浮亏5亿港元
Qualcomm Platform Development Series Explanation (Application) Introduction to QCMAP Application Framework
QT笔记——用VS + qt 生成dll 和 调用生成的dll
如何成为一名正义黑客?你应该学习什么?
罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
fme csmapreprojector转换器使用高程异常模型进行高程基准转换
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
Translating scientific and technological papers, how to translate from Russian to Chinese
随机推荐
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
BM13 determines whether a linked list is a palindrome
file IO-buffer
企业云存储日常运行维护实践经验分享
LeetCode每日两题01:反转字符串 (均1200道)方法:双指针
shell脚本
STL-stack
Translating scientific and technological papers, how to translate from Russian to Chinese
今日睡眠质量记录75分
PyQt5 窗口自适应大小
leetcode:355. 设计推特
【640. Solving Equations】
文件IO-缓冲区
美味石井饭菜
Black cat takes you to learn Makefile Part 12: Summary of common Makefile problems
How many threads does LabVIEW allocate?
水果沙拉酱
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
爬虫request.get()出现错误
IM 即时通讯开发如何设计图片文件的服务端存储架构