Python  bit mask

11.the man behind the bit mask
1.define a function,check_bit4,with one argument,input,a integer
2.it should check to see if the fourth bit from the right is on
3.if the bit is no,return "on"(not print)
4.if the bit is off,return "off"
def check_bit4(input):
mask =0b1000
desired = input & mask
if desired > 0:
return "on"
else:
return "off"
example:

num = 0b1100
mask =0b0100
desired = num & mask
if desired > 0:
print "bit was on"
we want to see if the third bit from the right is on.
1.first,we first create a varible num containing the number 12,or 0b1100
2.next,we create a mask with the third bit on.
3.then,we use a bitwise and operation to see if the third bit from the right of numis on
4.if desired is greater than zero,then the third bit of num must have been one.

the bitwise AND(&) operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if the corresponding bits of both numbers are 1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,507评论 0 23
  • “我没听错吧,你敢这么说我?”王泽宇脸色铁青的说道,此刻他正在夏兰旁边,刚刚王小天说的话无疑让夏兰听到了。这也就算...
    少爷喝酒阅读 1,580评论 0 0
  • 醒来以后就删了昨晚的链接,不在乎说有谁看过,有谁了解过,只是找一个情绪的发泄口! 早上4点20分自然醒,昨晚睡的很...
    宁湘elf阅读 3,685评论 0 0
  • 【投稿】2.3.2.1 电阻,电容,二极管,晶体管 【作者】0106 _刘杰 ;0121_ 丁爽 常用元件库介绍 ...
    0106_刘杰阅读 4,753评论 1 1
  • 我惊讶于我内心的空白
    我亦飘零久ss阅读 1,823评论 0 0

友情链接更多精彩内容