Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中如何根据位置获取十六进制数的位值_Python - Fatal编程技术网

python中如何根据位置获取十六进制数的位值

python中如何根据位置获取十六进制数的位值,python,Python,嗨,朋友们,我是Python语言的新手。我正在尝试编写一些小代码,稍后将它们集成到我的python模块中。这是我的问题 我在文件中有一个类似于a[31:0]=0X00010001的数字,我希望根据位置获取位值,并根据其位值执行操作。 我想使用一些类似于通用类的东西,如果一个数字也超过32位,我正在寻找解决方案。 0x00010001.getbit(0)==0执行一些操作。如果位0为0,则应为do操作 还有一个问题,如果我有相应的掩码位,我如何根据掩码位获得值 0x00010001在这种情况下,位

嗨,朋友们,我是Python语言的新手。我正在尝试编写一些小代码,稍后将它们集成到我的python模块中。这是我的问题

  • 我在文件中有一个类似于
    a[31:0]=0X00010001
    的数字,我希望根据位置获取位值,并根据其位值执行操作。 我想使用一些类似于通用类的东西,如果一个数字也超过32位,我正在寻找解决方案。
    0x00010001.getbit(0)==0
    执行一些操作。如果位0为0,则应为do操作

  • 还有一个问题,如果我有相应的掩码位,我如何根据掩码位获得值
    0x00010001
    在这种情况下,
    位[15]
    是掩码,
    位[0]
    是值

  • 谢谢你的帮助

    我已尝试通过bitstream模块执行此操作,但对于第一个问题,未成功

    def getbit(x,n):
      "Get the n-th bit of the number x"
      return x & (1 << n) and 1 or 0
    
    def getbit(x,n):
    “获取数字x的第n位”
    
    返回x&(1
    True,1
    returns 1Correct。我修复了帖子:-)谢谢Levin,我将尝试
    操作符的定义如下:
    x和y
    返回
    x
    x
    为False,否则为
    y
    的定义如下:
    x或y
    如果x为False,则返回
    y
    ,否则返回
    x
    。所以写
    p和1或0类似于在
    C
    中写
    p?1:0